0
x = [4, 5, 6]
li = [1, 2, 3, 7]
li.insert(3,x)
x+=li
print(x)

The output is:

[4, 5, 6, 1, 2, 3, [...], 7]

I'm new to python/coding and I don't know what these ellipses are but when I do other code it starts getting weird. Wasn't sure what to ask since I have no clue what's going on. Thank you!

Doej
  • 13
  • 1

1 Answers1

1

you're inserting a list inside your list, probably not what you want.

Then when doing this

x+=li

the representation of the list then shows an ellipsis because you're referencing the list from itself (x is referenced in li already)

To insert several items at once in a list in-place you could use slice assignment:

>>> x = [4, 5, 6]
>>> li = [1, 2, 3, 7]
>>> li[3:3] = x
>>> li
[1, 2, 3, 4, 5, 6, 7]
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • thank u ! so what I gather is that the insert function I used is actually referencing from 'x' itself and not simply taking its value ? thanks – Doej Nov 15 '17 at 10:13
  • because that's how python works: everything is reference, and lists are mutable. If you want to do this just use a copy of `x`: `x[:]` or `list(x)` – Jean-François Fabre Nov 15 '17 at 10:18