0

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)
    #prints None

but when I do it this way, it works exactly as I would expect.

    aList=[1,2,3,4,5]
    aList.remove(1)
    newList = aList
    print(newList)
    #prints [2,3,4,5]
  • That method is simply defined to not return anything. Are you asking why that decision was made, or whether there is a method that removes an element and *does* return something? – Undo Dec 05 '15 at 03:46
  • http://stackoverflow.com/questions/9983254/python-list-functions-not-returning-new-lists – Cyclonecode Dec 05 '15 at 03:48

1 Answers1

3

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.

pushkin
  • 9,575
  • 15
  • 51
  • 95