• How to remove none from list python (5 Ways)

    remove none from list Python

    In this article, we are solving the problem of how to remove none from list python. This type of problem in Python can be solved in many ways, we will be looking in 5 ways to remove none from list in python.

    None is a keyword in Python used to define a null value or no value at all. It is not the same as an empty string or zero, or false value, it is the data type of the class NoneType in Python.

    We can declare none variable as –

    a = None
    print("Value of a is", a)
    print("Type of a is", type(a))

    Output:-

    Value of a is None
    Type of a is <class ‘NoneType’>

    Why we should remove none from list Python?

    When analyzing large set of data, it likely that we come across with none values or missing values. To make sure data is clean and tidy for data analysis and data representation removing null values from list data in python is important.

    Since null values affect the performance and accuracy of machine learning algorithms it is necessary to handle these none values from data sets before applying the machine learning algorithm. Removing such unwanted null values, corrupting data before processing the algorithm is called data preprocessing.

    How to remove none from list Python

    The 5 Ways to get rid of none in Python are as follows-

    Method 1- Remove none from list using filter() method

    The easiest way to remove none from list in Python is by using the list filter() method. List filter() method takes two parameters as function and iterator. To remove none values from the list we provide none as the function to filter() method and the list which contains none values.

    Syntax –

    filter(none, iterator)

    None – To eliminate none values we provide none as the function in the filter method

    Iterator – An iterator like list, set, or tuple.

    Python Code:

    # List with none values
    sample_list = [1,2, True, None, False, None, 'Python', None]
    
    # Using filter() method to filter None values
    filtered_list = list(filter(None, sample_list))
    
    print("List with None values: ", sample_list)
    print("List without None values", filtered_list)

    Output:-

    List with None values: [1, 2, True, None, False, None, ‘Python’, None]
    List without None values [1, 2, True, ‘Python’]

    Method 2- Naive Method

    We can also remove none from list in python using a for loop and check whether the values are none if yes ignore it and the rest of the values can be appended to another list.

    Python Code:

    # List with none values
    sample_list = [1,2, True, None, False, None, 'Python', None]
    
    # initialize filtered list
    filtered_list = []
    
    # Using for loop
    for ele in sample_list:
        if ele != None:
            filtered_list.append(ele)
            
    print("List with None values: ", sample_list)
    print("List without None values", filtered_list)
    

    Output:-

    List with None values: [1, 2, True, None, False, None, ‘Python’, None]
    List without None values [1, 2, True, ‘Python’]

    Method 3- Using For loop + list remove()

    To remove none from list python we will iterate over the list elements and compare each element with none, if we find any none element we will remove none element from list using the python list remove() method.

    List remove() method takes a single lament as an argument and removes that element from the list.

    Syntax-

    list.remove(element)

    Python Code:

    # List with none values
    sample_list = [1,2, True, None, False, None, 'Python', None]
    
    #Printing define list
    print("List with None values:", sample_list)
    
    # Using for loop and remove method
    for ele in sample_list:
        if ele == None:
            sample_list.remove(ele)
    
    # print list after removing none
    print("List without None values:", sample_list)
    

    Output:-

    List with None values: [1, 2, True, None, False, None, ‘Python’, None]
    List without None values [1, 2, True, ‘Python’]

    Method 4- Using List Comprehension

    To remove none values from list we also use list comprehension. In Python, list comprehension is a way to create a new list from existing other iterables like lists, sets, and tuples. We can also say that list comprehension is a shorter version of for loop. To know about list comprehension you can visit this.

    Python Code:

    # List with none values
    sample_list = [1,2, True, None, False, None, 'Python', None]
    
    # Using List comprehension to remove none values
    filtered_list = [ ele for ele in sample_list if ele is not None ]
    
    print("List with None values: ", sample_list)
    print("List without None values", filtered_list)
    

    Output:-

    List with None values: [1, 2, True, None, False, None, ‘Python’, None]
    List without None values [1, 2, True, ‘Python’]

    Method 5- Using Filter() + lambda function

    In python, the lambda function is an anonymous function that is without any name. It takes any number of arguments but can only perform one expression.

    As we discuss above filter() method takes a function as input, and since lambda is also one kind of method in python, hence we provide lambda function and an iterator as input arguments to the filter method.

    Example - filter(lambda_function, iterator)

    Python code:

    # List with none values
    sample_list = [1,2, True, None, False, None, 'Python', None]
    
    # Using filter() method + lambda
    filtered_list = list(filter(lambda ele:ele is not None, sample_list))
    
    print("List with None values: ", sample_list)
    print("List without None values", filtered_list)

    Output:-

    List with None values: [1, 2, True, None, False, None, ‘Python’, None]
    List without None values [1, 2, True, ‘Python’]

    Conclusion

    Hence we have seen how to remove none from list python in 5 different ways. We can remove none from list python by using filter(), naive method, for loop + remove(), using list comprehension, and filter() + lambda function.