Home > Mobile >  .remove() in Python list gives the output "None"
.remove() in Python list gives the output "None"

Time:01-05

I have a list of values and I need to remove a specific one, however I end up getting the output "None", instead of a new list without the removed element

my_list = [(1, (255, 0, 255)), (10, (125, 0, 125)), (20, (0, 255, 255)), (28, (255, 0, 0)), (31, (125, 0, 255))]
new_list = my_list.remove((1, (255, 0, 255)))
print(new_list)

the output I get: None

the output I would like: [(10, (125, 0, 125)), (20, (0, 255, 255)), (28, (255, 0, 0)), (31, (125, 0, 255))]

The problem might be in the way it is formatted, however I get these results from somewhere, I didn't choose to write it like this. In that case how do I reformat the list?

CodePudding user response:

remove function modifies a list but always returns None. You can do something like this:

my_list = [(1, (255, 0, 255)), (10, (125, 0, 125)), (20, (0, 255, 255)), (28, (255, 0, 0)), (31, (125, 0, 255))]
my_list.remove((1, (255, 0, 255)))
print(my_list)

CodePudding user response:

Actually the remove() method returns None, it is documented here

https://docs.python.org/3/tutorial/datastructures.html#:~:text=You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed – they return the default None. 1 This is a design principle for all mutable data structures in Python.

  •  Tags:  
  • Related