Remove None Value from a List Without Removing the 0 Value

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]

Remove empty strings from a list of strings

I would use filter:

str_list = filter(None, str_list)
str_list = filter(bool, str_list)
str_list = filter(len, str_list)
str_list = filter(lambda item: item, str_list)

Python 3 returns an iterator from filter, so should be wrapped in a call to list()

str_list = list(filter(None, str_list))

Replace None with incremental value excluding values from skip list

You're looking up the wrong element in the cache in a couple places, and not skipping properly when the skip list contains consecutive elements. Here's the minimal fix with inline comments indicating changed lines:

skip = [1,2]
a = [[1, None, 2], [3, 4, 5], [1, None, 7], [8, 9, 10],[11, None, 12]]
b = 0
d = {}
for i in range(len(a)):
if a[i][1]==None:
while b in skip: # Loop until skipped all elements in skip
print("found b in skip")
b = b + 1
if a[i][0] in d.keys(): # Check for first, not second element
a[i][1] = d[a[i][0]] # Use first element, not second, for lookup
else:
a[i][1] = b
d[a[i][0]] = b # Indent so we only set cached value on cache miss
b = b + 1 # Indent so we only increment b on new first element
print(d)
print(a)

Try it online!

And here's a more heavily modified version that is somewhat more Pythonic, using names, not indexing (when possible):

skip = {1,2}  # Use set instead of list; if skip is large, list membership check will be expensive, set will stay cheap
a = [[1, None, 2], [3, 4, 5], [1, None, 7], [8, 9, 10],[11, None, 12]]
b = 0
d = {}
for sublist in a: # Loop over values instead of indexing a over and over, C-style (slow and less readable)
first, middle, _ = sublist # Unpack to useful names (reducing risk of misindexing, and making more readable code)
# Not unpacking in for loop itself because we need to reassign elements of sublist
if middle is None: # Always use is/is not to compare to None
while b in skip:
b += 1 # Use += to avoid repeating variable name
if first in d: # No need for .keys(); "in d" has same effect as "in d.keys()" and avoids creating unnecessary keys view
sublist[1] = d[first] # Indexing needed for assignment to modify original list
else:
sublist[1] = d[first] = b # Can populate cache and reassign middle value all at once
b += 1
print(d)
print(a)

Try it online!

Either version gets the expected output from the question.

False' is removed from the list when removing other elements from list

False and True are equal to 0 and 1 respectively. If you want to remove 0 from a list without removing False, you could do this:

my_list = [0,1,None,2,False,1,0]

my_list_without_zero = [x for x in my_list if x!=0 or x is False]

Since False is a single object, you can use is to check if a value is that specific object.

What is the purpose of doing my_list = list(filter(None, my_list))

This will filter out falsy values from my_list and return a new list. It's probably best illustrated with an example:

my_list = [1, 2 , 0, 3, None, 4, False, True, [], [1], '', 'abc']

list(filter(None, my_list))
# [1, 2, 3, 4, True, [1], 'abc']

The documentation explains it:

filter(function, iterable)

...

If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.



Related Topics



Leave a reply



Submit