What does the list() function do in Python?

list() converts the iterable passed to it to a list. If the itertable is already a list then a shallow copy is returned, i.e only the outermost container is new rest of the objects are still the same.

>>> t = (1,2,3,4,[5,6,7,8],9)
>>> lst = list(t) 
>>> lst[4] is t[4]  #outermost container is now a list() but inner items are still same.
True

>>> lst1 = [[[2,3,4]]]
>>> id(lst1)
140270501696936
>>> lst2 = list(lst1)
>>> id(lst2)
140270478302096
>>> lst1[0] is lst2[0]
True

Leave a Comment