w3resource

Python: Remove None value from a given list using lambda function

Python Lambda: Exercise-52 with Solution

Write a Python program to remove None value from a given list using lambda function.

Sample Solution:

Python Code :

def remove_none(nums):
    result = filter(lambda v: v is not None, nums)
    return list(result)

nums = [12, 0, None, 23, None, -55, 234, 89, None, 0, 6, -12]
print("Original list:")
print(nums)
print("\nRemove None value from the said list:")
print(remove_none(nums))

Sample Output:

Original list:
[12, 0, None, 23, None, -55, 234, 89, None, 0, 6, -12]

Remove None value from the said list:
[12, 0, 23, -55, 234, 89, 0, 6, -12]

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the maximum and minimum values in a given list of tuples using lambda function.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

Python: Tips of the Day

Memoization using LRU cache:

import functools

@functools.lru_cache(maxsize=128)
def fibonacci(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    return fibonacci(n - 1) + fibonacci(n - 2)