x = [1,2,3]
y = x
print(id(x), id(y))
Output
3121052975624 3121052975624
x = [1,2,3]
y = x
print(id(x), id(y))
Output
3121052975624 3121052975624
x = [1,2,3]
y = x
x[0] = 10
print(x, y)
Output
[10, 2, 3] [10, 2, 3]
l1 = [1,2,3]
l2 = list(l1) # 1. call list constructor
l3 = l1[:] # 2. copy all elements via slicing
l4 = l1.copy() # 3. call list copy function
l1[0] = 10
print(l1)
print(l2)
print(l3)
l1 = [[10,20,30], 1, 2, 3]# a list with another list at index 0
l2 = list(l1) # create a copy
l1.append(4) # append to list l1
l1[0].append(40) # modify the inner list of l1
print(l1)
print(l2)
l1 = [[10,20,30], 1, 2, 3]# a list with another list at index 0
l2 = list(l1) # create a copy
l1.append(4) # append to list l1
l1[0].append(40) # modify the inner list of l1
print( id(l1[0]) )
print( id(l2[0]) )
import copy # this is the module to use
l1 = [[10,20,30], 1, 2, 3]
l2 = copy.deepcopy(l1) # create a copy using the deepcopy function from copy
l1.append(4) # append to list l1
l1[0].append(40) # modify the inner list of l1
print( l1 )
print( l2 )
``` | |
l1 = [ 1,2,3 ] | |
l2 = [ l1, [10,20,30] ] | |
l3 = [ list(l1), [10,20,30] ] | |
l1.append(4) | |
l2[1].append(40) | |
print(l1) | |
print(l2) |
A = [ 1 ,2 ,3 ,4 ] # list | |
A = ( 1 ,2 ,3 ,4 ) # tuple |
A=[ 1 ,2 ,3 ,4 ]
print(A[0])
print(A[1])
print(A[2])
print(A[3])
Output
1
A = [ 1 ,2 ,3 ,4 ]
for i in range( len(A) ): # uses indexes
print(A[i])
Output
1
2
3