python - Why does appending a list to itself show [...] when printed? -
when appended list in using following code.
a = [1, 2, 3, 4] print a.append(a) print
i expecting output be...
[1, 2, 3, 4] [1, 2, 3, 4, [1, 2, 3, 4]]
but this...
[1, 2, 3, 4] [1, 2, 3, 4, [...]]
why?
you adding a
a
itself. second element of a
a
only. so, if tries print a
, wanted
- it print first element
[1, 2, 3, 4]
it print second element,
a
- it print first element
[1, 2, 3, 4]
it print second element,
a
- it print first element
[1, 2, 3, 4]
- it print second element,
a
...
- it print first element
- it print first element
you see how going, right? doing infinitely. when there circular reference this, python represent ellipsis within pair of square brackets, [...]
, only.
if want insert copy of a
is, can use slicing create new list, this
>>> = [1, 2, 3, 4] >>> a.append(a[:]) >>> [1, 2, 3, 4, [1, 2, 3, 4]]
Comments
Post a Comment