1

Wondering what I can infer from the below statements?

>>> li=['a', 'b']
>>> id(li)
4300601032
>>> li.insert(0,'Z')
>>> li
['Z', 'a', 'b']
>>> id(li)
4300601032

Is the original list object copied before inserting 'Z' or does it work like a linked-list?

user1484282
  • 143
  • 1
  • 6

1 Answers1

1

As documented:

List and bytearray objects support additional operations that allow in-place modification of the object.

insert is listed as one of those operations. So no copy is made, and insert modifies the list object in place.

Note that this has no bearing on how lists are implemented. Whether lists are implemented as linked lists is not part of what is specified by the documentation. All that is specified is what the various operations do, not how they are done.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384