Removing Item from List Causes the List to Become Nonetype

Removing item from list causes the list to become NoneType

remove doesn't return anything. It modifies the existing list in-place. No assignment needed.

Replace

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

with

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

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

Remove NoneType from two lists

I would do it like this

np_array[np_array != np.array(None)]

The statement np_array != np.array(None) outputs a boolean array. This boolean array would have 'False' where ever the element is of 'None' type and True for others. Indexes corresponding to 'True' remain in the resultant array and those corresponding to 'False' is removed.

Applying this to your problem

ToKeep = (ListA != np.array(None))
ListA = ListA[ToKeep]
ListB = ListA[ToKeep]

Appending turns my list to NoneType

list.append is a method that modifies the existing list. It doesn't return a new list -- it returns None, like most methods that modify the list. Simply do aList.append('e') and your list will get the element appended.

TypeError: object of type 'NoneType' has no len() after using remove() on a list

list.remove is an in-place operation. It returns None.

You need to perform this operation in a separate line to new_list. In other words, instead of new_list = list_of_directions.remove('right'):

new_list = list_of_directions[:]

new_list.remove('right')

In the above logic, we assign new_list to a copy of list_of_directions before removing a specific element.

Notice the importance of assigning to a copy of list_of_directions. This is to avoid the highly probable scenario of new_list changing with list_of_directions in an undesired manner.

The behaviour you are seeing is noted explicitly in the docs:

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. This is a design principle for all mutable data
structures in Python.

remove None value from a list without removing the 0 value

>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> [x for x in L if x is not None]
[0, 23, 234, 89, 0, 35, 9]

Just for fun, here's how you can adapt filter to do this without using a lambda, (I wouldn't recommend this code - it's just for scientific purposes)

>>> from operator import is_not
>>> from functools import partial
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> list(filter(partial(is_not, None), L))
[0, 23, 234, 89, 0, 35, 9]

How can I remove None values of one list and the equivalent values from another?

The problem is caused because you're deleting items from the list while looping. So when you start the loop the length of the list was 7, but because you deleted one item in the middle, the loop goes out of index in the final stage. Lesson: Don't delete from list while using it in a loop.

But here is a way to do it without causing error

list1 = [2,3,4,None,2,4,5]
list2 = ['red','blue','white','red','blue','blue','blue']
for i, val in enumerate(list1):
if val is None:
del list1[i]
del list2[i]

Native Python function to remove NoneType elements from list?

I think the cleanest way to do this would be:

#lis = some list with NoneType's
filter(None, lis)


Related Topics



Leave a reply



Submit