Why I cannot apply .remove() method to list in python?

Question:

Actual code:

my_list = ['a', 'b', 'c']
no_c_list = my_list.remove('c')
print(no_c_list)

The expected output is:

[‘a’, ‘b’]

But in reality, I’m getting None:
enter image description here

What I’m doing wrong and what should I do to remove the item from the list?

Asked By: Quanti Monati

||

Answers:

remove() does not return anything. Or, perhaps to be more precise, it returns None. You’re removing 'c' from the my_list and then setting the no_c_list to None.

my_list = ['a', 'b', 'c']
my_list.remove('c')
print(my_list)
Answered By: Michael Bianconi

You are using the .remove() function incorrectly. The remove() function removes the element from the my_list, it doesn’t create a new list that doesn’t have the element. Try this

my_list = ['a', 'b', 'c']
my_list.remove('c')
print(my_list)

https://repl.it/repls/ElementaryCourageousRadius

Answered By: Sheshank S.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.