Removing Tuples from a List by First Element Value in Python

Given a list of tuples, we need to delete tuples based on a specific condition related to their first element. In this article, we will explore three different approaches to deleting a tuple from a list of tuples based on the first element in Python.

Delete A Tuple From A List Of Tuples Based On The First Element

We are given a Python list of tuples and a condition for the first element of the tuple. If the condition matches and returns True, that tuple is to be deleted from the list of tuples. Let us see an example to understand the problem statement.

Example:

Input: [("w3wiki", "Python", 1000),  ("w3wiki", "JavaScript", 1500),    
("CodingForAll", "Java", 1200), ("w3wiki", "C++", 1300),
("CodeWars", "Python", 1100)]

Condition: first element should be "w3wiki"

Output: [("CodingForAll", "Java", 1200), ("CodeWars", "Python", 1100)]

Methods to Removing Tuples from a List by First Element Value

Below are the approaches to deleting a tuple from a list of tuples based on the first element in Python.

  • Using List Comprehension
  • Using filter() and Lambda
  • Using For Loop and append()
  • Using For Loop and remove()

Now let us see each of these method one by one with code examples.

Using List Comprehension

Python list comprehension is a technique of creating a new list by solving an expression which is executed for each iterable within the list. By iterating through the list and including only those tuples whose first element does not match “w3wiki“, we remove the unwanted tuples from the list.

Example: In this example, we are using list comprehension to filter out tuples. We are iterating each element of the list that is a tuple, and if the tuple’s first element is not “w3wiki“, we are adding that tuple in the ‘data’ list.

Python
data = [
    ("w3wiki", "Python", 1000),
    ("w3wiki", "JavaScript", 1500),
    ("CodingForAll", "Java", 1200),
    ("w3wiki", "C++", 1300),
    ("CodeWars", "Python", 1100)
]

data = [tup for tup in data if tup[0] != "w3wiki"]
print(data)

Output:

[('CodingForAll', 'Java', 1200), ('CodeWars', 'Python', 1100)]

Using filter() and Lambda

Python filter() function is used to filter out some data based on a specific condition where as lambda functions are used when there is a simple and short operation to be performed. Using these two function together we can filter out a tuple from the list. The lambda function checks each tuple, and the filter() retains only those tuples that do not meet the condition, that is the element is not “w3wiki”, removing the unwanted tuples from the list.

Example: In this example, we are using the lambda function to iterate over each element in the list and then filter() function is removing all those elements where the condition returns False. Lastly the list() function is converting the iterable back to a list.

Python
data = [
    ("w3wiki", "Python", 1000),
    ("w3wiki", "JavaScript", 1500),
    ("CodingForAll", "Java", 1200),
    ("w3wiki", "C++", 1300),
    ("CodeWars", "Python", 1100)
]

data = list(filter(lambda x: x[0] != "w3wiki", data))
print(data)

Output:

[('CodingForAll', 'Java', 1200), ('CodeWars', 'Python', 1100)]

Using For Loop and append() Function

A for loop in python is used to iterate over each element in a sequence, in this case a list. Then using if condition on each element that is a tuple extracted from the for loop, we will check if the first element of the tuple does not match the condition, that is the first element of the tuple is not “w3wiki”, we will add that tuple in a new list using list append() function.

Example: In this example, we will use a for loop to iterate over each element of the list. And then append the data in a new list if the condition returns True.

Python
data = [
    ("w3wiki", "Python", 1000),
    ("w3wiki", "JavaScript", 1500),
    ("CodingForAll", "Java", 1200),
    ("w3wiki", "C++", 1300),
    ("CodeWars", "Python", 1100)
]

# new list
new_data = []
for tup in data[:]:
    if tup[0] != "w3wiki":
        new_data.append(tup)
print(new_data)

Output:

[('CodingForAll', 'Java', 1200), ('CodeWars', 'Python', 1100)]

Using For Loop and remove() Function

This method is similar to the previous method with only two exception. First is the if condition, where the first element of each tuple is “w3wiki“. And the second exception is, instead of append() function, we will use list remove() function which will remove only those tuples from the original list where the condition evaluates to be True.

Example: In this example, we will iterate over each element of the list using for loop and then remove those tuples whose first element match the condition using the list remove() function.

Python
data = [
    ("w3wiki", "Python", 1000),
    ("w3wiki", "JavaScript", 1500),
    ("CodingForAll", "Java", 1200),
    ("w3wiki", "C++", 1300),
    ("CodeWars", "Python", 1100)
]

for tup in data[:]:
    if tup[0] == "w3wiki":
        data.remove(tup)
print(data)

Output:

[('CodingForAll', 'Java', 1200), ('CodeWars', 'Python', 1100)]