2
    >>> a = [1,2,3]
    >>> b = [1,2]
    >>> a.append(b)
    >>> a
    [1, 2, 3, [1, 2]]
    >>> b
    [1, 2]
    >>> b.extend(a)
    >>> b
    [1, 2, 1, 2, 3, [...]]
    >>> b[5]
    [1, 2, 1, 2, 3, [...]]

What is the [...] here? This is confusing me. What's wrong with my approach? Can someone clarify my doubt?

Hexaholic
  • 3,299
  • 7
  • 30
  • 39
rajpython
  • 179
  • 2
  • 3
  • 14

1 Answers1

3

You created a circular reference. a contains the same list b was referencing. By extending b with a, b now contains a reference to itself.

Python displays such a reference by using ... rather than go into an infinite loop. Printing b[5] is printing the same object again, so the output naturally is the same again.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343