Signup/Sign In
LAST UPDATED: MARCH 23, 2023

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

Technology #tuple#python#list

    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 learned how to remove a specific element from a list of tuples in Python. The methods discussed here are effective and efficient in achieving this requirement. We hope this tutorial was helpful and welcome any suggestions or feedback you may have. Let us know your approach to solving this problem in the comments below!

    Frequently Asked Questions(FAQs)

    1) How do you remove the first element of a list of tuples?

    You can use the del keyword in Python to removethe first element. For Example, we have this list of tuples in Python and we have to remove the first element then we can simply specify the index of the first element which is "0":

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

    2) How to remove the last element of a list of tuples in Python?

    The pop () method can be used to remove the last element from a list of tuples in Python. You can check out this entire post also for the example.

    3) What is the difference between removing an element from a list and a tuple in Python?

    Lists in Python are mutable, so elements can be removed using the remove() method or del keyword. Tuples are immutable, so elements cannot be removed, but instead, a new tuple must be created.

    4) How do you check if an element exists in a tuple in Python?

    The "in" expression in Python can be used to determine whether an element appears in a tuple. If the constituent exists in the collection, the formula returns True.

    You may also like:

    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS