0

I would like to know what the difference is between the last two list assignments below

List1 = [0x1,0x2,0x3,0x4]
print('{}, 0x{:X}'.format(List1, id(List1)))
List1 = List1[1:]
print('{}, 0x{:X}'.format(List1, id(List1)))
List1[:] = List1[1:]
print('{}, 0x{:X}'.format(List1, id(List1)))

The print out when I execute this is

[1, 2, 3, 4], 0x2DBA09E8C88
[2, 3, 4], 0x2DBA1342B48
[3, 4], 0x2DBA1342B48

After I have created the list.

I make what I believe is a shallow copy.

But what does the last assignment do ? It keeps its address so there is not created a new object.

I cant seem to find en explanation on this on this in the net, what should I look for

Regards

Ephreal
  • 1,835
  • 2
  • 18
  • 35
  • Does this answer your question? [How assignment works with Python list slice?](https://stackoverflow.com/questions/10623302/how-assignment-works-with-python-list-slice) Some more helpful links: [Mutable Sequence Types](https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types), [Assignment statements](https://docs.python.org/3/reference/simple_stmts.html#assignment-statements) – Tomerikoo Jan 21 '20 at 09:57
  • The answer did help me. – Ephreal Feb 26 '20 at 07:40

0 Answers0