1

I created a list called a, when I inserted a at the beginning of the list this happened

>>> a = ['a', 'b', 'c', 'd']
>>> a.insert(0, a)
>>> a
[[...], 'a', 'b', 'c', 'd']
vd743r
  • 13
  • 2

2 Answers2

1

Running this code:

a = ['a', 'b', 'c', 'd']
a.insert(0, a)
print(a)
print(a[0])

yields this result:

[[...], 'a', 'b', 'c', 'd']
[[...], 'a', 'b', 'c', 'd']

this leads me to believe this is how python handles printing recursive referencing. a contains a reference to a, which contains a reference to a... meaning there's no good way to actually print it, so it just prints [...]


if you want to include the values of a inside of a pointer to a, you might want to copy it

this code:

import copy
a = ['a', 'b', 'c', 'd']
a.insert(0, copy.deepcopy(a))
print(a)

results in this:

[['a', 'b', 'c', 'd'], 'a', 'b', 'c', 'd']
Warlax56
  • 1,170
  • 5
  • 30
1

[...] stands for the list itself that you previously added.

MatiasGSP
  • 75
  • 7