Skip to content Skip to sidebar Skip to footer

Why Does Newlist = List.remove(x) Return None?

I have a list below and I want to remove a value and set it to a new variable. Why does it return None? aList=[1,2,3,4,5] newList = aList.remove(1) print(newList) #

Solution 1:

In Python, when a function mutates an object it generally returns None.Otherwise it returns the new object.

remove mutates the original object, which is why when you do aList.remove(1) and then print the list, you will see that the list is different.

Post a Comment for "Why Does Newlist = List.remove(x) Return None?"