Python trick - How to make lists not share the same memory location

You need to copy the original L2 list as L1 list and save it.

But, you don’t want the L1 list to change when the L2 list changes.

Doing this as follows will change the members in both lists, because lists L1 and L2 share the same memory location:

L2 = [0, 1, 2, 3, 4, 5]
L1 = L2

print('\nAfter copying list L2 to list L1')
print('L1 and L2 share the same memory location')
print('\nL1 =', L1, ',    L2 =', L2)
print('id(L1) =', id(L1), ',  id(L2) =', id(L2))

L2.append(6)
print('\nAfter adding the last member in the L2 list')
print('L1 =', L1, ',    L2 =', L2)
print('id(L1) =', id(L1), ',  id(L2) =', id(L2))

The id function shows us the memory location of the list. A new member was added to the end of the list L2, but Python automatically added a new member to the end of the list L1, because the lists share the same memory location because it is written:

L1 = L2

Display on screen:

After copying list L2 to list L1
L1 and L2 share the same memory location

L1 = [0, 1, 2, 3, 4, 5] ,    L2 = [0, 1, 2, 3, 4, 5]
id(L1) = 2174072709184 ,  id(L2) = 2174072709184

After adding the last member in the L2 list
L1 = [0, 1, 2, 3, 4, 5, 6] ,    L2 = [0, 1, 2, 3, 4, 5, 6]
id(L1) = 2174072709184 ,  id(L2) = 2174072709184

The same thing happens if we delete, for example, the last member in list L2 - the last member in list L1 will be automatically deleted:

L2 = [0, 1, 2, 3, 4, 5]
L1 = L2
print('\nAfter copying list L2 to list L1')
print('L1 and L2 share the same memory location')
print('\nL1 =', L1, ',    L2 =', L2)
print('id(L1) =', id(L1), ',  id(L2) =', id(L2))

del(L2[-1])
print('\nAfter deleting the last member in the L2 list')
print('L1 =', L1, ',    L2 =', L2)
print('id(L1) =', id(L1), ',  id(L2) =', id(L2))

Display on screen:

After copying list L2 to list L1
L1 and L2 share the same memory location

L1 = [0, 1, 2, 3, 4, 5] ,    L2 = [0, 1, 2, 3, 4, 5]
id(L1) = 2093463802368 ,  id(L2) = 2093463802368

After deleting the last member in the L2 list
L1 = [0, 1, 2, 3, 4] ,    L2 = [0, 1, 2, 3, 4]
id(L1) = 2093463802368 ,  id(L2) = 2093463802368

If you don’t want this to happen, apply the trick:

L1 = L2+[]

or

L1 = []+L2

Let’s see what happens now:

L2 = [0, 1, 2, 3, 4, 5]
L1 = L2+[]
print('\nAfter copying list L2 to list L1')
print('L1 and L2 do not share the same memory location')
print('\nL1 =', L1, ',    L2 =', L2)
print('id(L1) =', id(L1), ',  id(L2) =', id(L2))

L2.append(6)
print('\nAfter adding the last member in the L2 list')
print('L1 =', L1, ',    L2 =', L2)
print('id(L1) =', id(L1), ',  id(L2) =', id(L2))

Display on screen:

After copying list L2 to list L1
L1 and L2 do not share the same memory location

L1 = [0, 1, 2, 3, 4, 5] ,    L2 = [0, 1, 2, 3, 4, 5]
id(L1) = 2093432366464 ,  id(L2) = 2093463803840

After adding the last member in the L2 list
L1 = [0, 1, 2, 3, 4, 5] ,    L2 = [0, 1, 2, 3, 4, 5, 6]
id(L1) = 2093432366464 ,  id(L2) = 2093463803840

It can be seen that the lists do not share the same memory location!
The last member was added to list L2, but list L1 was not changed!
Likewise, when deleting the last member in list L2, the last member in list L1 will not be deleted:

L2 = [0, 1, 2, 3, 4, 5]
L1 = L2+[]
print('\nAfter copying list L2 to list L1')
print('L1 and L2 do not share the same memory location')
print('\nL1 =', L1, ',    L2 =', L2)
print('id(L1) =', id(L1), ',  id(L2) =', id(L2))

del(L2[-1])
print('\nAfter deleting the last member in the L2 list')
print('L1 =', L1, ',    L2 =', L2)
print('id(L1) =', id(L1), ',  id(L2) =', id(L2))

Display on screen:

After copying list L2 to list L1
L1 and L2 do not share the same memory location

L1 = [0, 1, 2, 3, 4, 5] ,    L2 = [0, 1, 2, 3, 4, 5]
id(L1) = 2093427033472 ,  id(L2) = 2093463801472

After deleting the last member in the L2 list
L1 = [0, 1, 2, 3, 4, 5] ,    L2 = [0, 1, 2, 3, 4]
id(L1) = 2093427033472 ,  id(L2) = 2093463801472

Instead of L1=L2+[] it could have been written:

for i in range(len(L2)):
    L1[i]=L2[i]

or

L1 = [L2[i] for i in range(len(L2))]

but when the lists are large, more execution time is unnecessarily spent.

You can find useful information about Python tricks (and other things) on the site:

Python&Math

0
Subscribe to my newsletter

Read articles from Miodrag Golubović directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Miodrag Golubović
Miodrag Golubović

Electrical engineer. He programs in several programming languages. Python because it's relatively lightweight, calculates with very large numbers, has great support and tons of pre-built stuff that you just need to include in your program.