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 ...

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

Popular posts from this blog

node.js - How to mock a third-party api calls in the backend -

node.js - Why do I get "SOCKS connection failed. Connection not allowed by ruleset" for some .onion sites? -

matlab - 0-by-1 sym - What do I need to change in order to get proper symbolic results? -