Signup/Sign In

Python | How to remove a specific element from a list of tuples?

Posted in Programming   LAST UPDATED: AUGUST 26, 2021

    We know what a tuple data structure in python (immutable) is and we also know what a list data structure is (mutable). But what is a list of tuples? A tuple list or list of tuples is simply a set of tuples (parentheses) enclosed within the list (square brackets).

    When we have a list of tuples, it might be required to remove a specific element from a particular tuple, but as a tuple is immutable, hence it wouldn't allow us to do so directly. But here we are dealing with a list with tuples stored in it. Hence, there are multiple ways to remove a specific element from a tuple stored inside a list.


    1. Using the pop() method

    The pop() method can be used with lists, not tuples. Also, only one argument needs to be supplied to this method at a time. Using this method, we can completely remove a tuple from a tuple list. We cannot use this function to remove a specific element from inside a tuple.

    Time for an example:

    my_tuple_list = [(1,2), (3.78, 9.56), ("StudyTonight", "Study")]
    my_tuple_list.pop(2)
    print(my_tuple_list)

    Output:

    [(1, 2), (3.78, 9.56)]


    2. Using del keyword

    The del keyword can be used along with the tuple list and passing the element's index to it. This method also deletes an entire tuple from the list.

    Time for an example:

    my_tuple_list = [(1,2), (3.78, 9.56), ("StudyTonight", "Study")]
    del my_tuple_list[0]
    print(my_tuple_list)

    Output:

    [(3.78, 9.56), ('StudyTonight', 'Study')]


    3. Using the remove() method:

    The remove() method can be used with a list or a tuple list and the index of the element to be removed can be passed as an argument to the remove method. This method also deletes an entire tuple from the list.

    Time for an example:

    my_tuple_list = [(1,2), (3.78, 9.56), ("StudyTonight", "Study")]
    element_to_be_removed = [my_tuple_list[1]]
    for element in element_to_be_removed:
        my_tuple_list.remove(element)
    print(my_tuple_list)

    Output:

    [(1, 2), ('StudyTonight', 'Study')]


    4. Using Numpy array and delete method

    The tuple list can be converted into a Numpy array. Once it is converted into a Numpy array, it behaves a little differently. Using the delete method to delete the element doesn't delete the entire tuple, but an element from a specific index only. In the previous examples, when the index was specified, the tuple at that index was deleted. But in this case, only the element at that index is deleted since it has already been converted into a Numpy array.

    Time for an example:

    import numpy as np
    
    my_tuple_list = [(1,2), (3.78, 9.56), ("StudyTonight", "Study")]
    my_tuple_list = np.array(my_tuple_list)
    my_tuple_list = np.delete(my_tuple_list, [1,2])
    print(my_tuple_list)

    Output:

    ['1' '9.56' 'StudyTonight' 'Study']


    5. Using list comprehension:

    A simple list comprehension can be used to remove a specific element from a tuple list by specifying a condition inside it.

    Time for an example:

    my_tuple_list = [(1,2), (3.78, 9.56), ("StudyTonight", "Study")]
    my_tuple_list = [tuple(ele for ele in sub if ele != 2) for sub in my_tuple_list] 
    print(my_tuple_list)

    Output:

    [(1,), (3.78, 9.56), ('StudyTonight', 'Study')]

    The above code example will remove the element in tuple with value 2.


    6. Using the filter() method

    The filter method has been explained in detail here. It can be used to filter out specific elements and display the elements which are still present in the tuple list.

    Time for an example:

    my_tuple_list = [(1,2), (3.78, 9.56), ("StudyTonight", "Study")]
    print(list(filter(lambda x: x[1] != 2, my_tuple_list)))

    Output:

    [(3.78, 9.56), ('StudyTonight', 'Study')]


    Conclusion:

    In this post, we understood how a specific element can be removed from a tuple list. Do let us know your approach to achieve this requirement.

    You may also like:

    Published by: SmritiSatyan
    Tags:PythonPython TuplePython List
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS