-1

I know that list is a mutable object in python. Then why does it assign a new location for a list variable when I concatenate list elements.Why doesn't myList add the contents on the original object instead of creating a new object?

list1=[1,2,3,4,5]
def proc(myList):
     myList = myList + [6, 7]
     print(myList)


 print (list1)
 proc(list1)
 print (list1)
metasj
  • 65
  • 1
  • 7

1 Answers1

3

Because assignment doesn't mutate, it rebinds.

myList.extend([6, 7])
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358