[Solved] List function not responding with a colon [closed]


This comes down to the difference between using y and y[:].

That colon in square brackets is a slice, however as you have no start or stop values, the slice is a “slice” of the whole list. This may seem identical to the original list, which it is, but only in value – behind the scenes, when you take any slice, you are given a copy of the list, not a reference to a section of it.

What this comes down to is that when you modify a copy of a list, the original isn’t effected. Which is why when you pass a copy of y with y[:] the result changes.


Consider the following which should illustrate how y and y[:] are not the same.

>>> l = [1,2,3]
>>> a = l
>>> b = l[:]
>>> a.append(4)
>>> b.append(5)
>>> l
[1, 2, 3, 4]
>>> a
[1, 2, 3, 4]
>>> b
[1, 2, 3, 5]
>>> id(l)
140460699987208
>>> id(a)
140460699987208
>>> id(b)
140460752668680

Notice how modifying a changes l as they point to the same memory location (illustrated by the id() function). But modifying b does not modify l as it is a reference to a different memory location – hence the result of id() is different.

solved List function not responding with a colon [closed]