This question has already been answered:

There is a piece of code:

def remitem(mylist): mylist2=mylist for item in mylist: if (mylist.count(item)==1): mylist2.remove(item) return mylist2 print remitem([5, 6, 7, 8, 9]) 

Please tell me why he returns [6,8], then the cycle goes through one list, and deletes it from another - according to logic, does it have to return []?

Reported as a duplicate by jfs python Jan 24 '17 at 3:59 pm

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

1 answer 1

because it is the same list, because it is mutable (you can read more by searching the Internet for this word)

if you need a copy of this list - use the copy command or mylist2 = mylist [:]

  • those. is mylist2 = mylist - just creating a link to the same memory area? - Asmodey
  • yes it is - actionless