Removing an item from the list causes the list to become unleaded

I assume there is a simple solution that I am missing. Better than complicated, right?

Simply put:

var = ['p', 's', 'c', 'x', 'd'].remove('d') 

calls var type None . What's going on here?

+6
source share
2 answers

remove returns nothing. It modifies the existing list in place. No appointment required.

Replace

 var = ['p', 's', 'c', 'x', 'd'].remove('d') 

with

 var = ['p', 's', 'c', 'x', 'd'] var.remove('d') 

Now var will have the value ['p', 's', 'c', 'x'] .

+14
source

remove mutates the list in place and returns None . You have to put it in a variable and then change this:

 >>> var = ['p', 's', 'c', 'x', 'd'] >>> var.remove('d') # Notice how it doesn't return anything. >>> var ['p', 's', 'c', 'x'] 
+1
source

Source: https://habr.com/ru/post/977790/


All Articles