Removing Tuples from a List by First Element Value - Python
Last Updated :
29 Jan, 2025
In this problem we need to delete tuples based on a specific condition related to their first element. For example: We are given the list data = [("GeeksforGeeks", "Python", 1000), ("CodingForAll", "Java", 1200)] and we need to remove all tuples where the first element is "GeeksforGeeks", the desired output would be data = [("CodingForAll", "Java", 1200)].
Using List Comprehension
List comprehension is a concise way to create am new list by filtering out elements based on a condition and that is why in this method we iterate through the list using list comprehension and keep only those tuples where the first element does not match the given condition.
Python
li = [
("GeeksforGeeks", "Python", 1000),
("GeeksforGeeks", "JavaScript", 1500),
("CodingForAll", "Java", 1200),
("GeeksforGeeks", "C++", 1300),
("CodeWars", "Python", 1100)
]
# Using list comprehension to filter out unwanted tuples
res = [tup for tup in li if tup[0] != "GeeksforGeeks"]
print(res)
Output[('CodingForAll', 'Java', 1200), ('CodeWars', 'Python', 1100)]
Explanation:
- List comprehension efficiently filters out tuples where the first element is "GeeksforGeeks."
- The condition tup[0] != "GeeksforGeeks" ensures that only tuples with a first element other than "GeeksforGeeks" are included in the result.
Using filter()
filter() function combined with a lambda function can be used to filter out elements based on a condition. In this method the lambda function checks whether the first element of the tuple is not equal to the specified value.
Python
li = [
("GeeksforGeeks", "Python", 1000),
("GeeksforGeeks", "JavaScript", 1500),
("CodingForAll", "Java", 1200),
("GeeksforGeeks", "C++", 1300),
("CodeWars", "Python", 1100)
]
# using filter and lambda to remove unwanted tuples
li = list(filter(lambda x: x[0] != "GeeksforGeeks", li))
print(li)
Output[('CodingForAll', 'Java', 1200), ('CodeWars', 'Python', 1100)]
Using For Loop and append() Function
In this method we use a for loop to iterate through the list li and if the condition is met (first element is not "GeeksforGeeks"), the tuple is appended to a new list using append().
Python
li = [
("GeeksforGeeks", "Python", 1000),
("GeeksforGeeks", "JavaScript", 1500),
("CodingForAll", "Java", 1200),
("GeeksforGeeks", "C++", 1300),
("CodeWars", "Python", 1100)
]
new_li = []
# iterating through the list
for tup in li:
if tup[0] != "GeeksforGeeks":
new_li.append(tup)
print(new_li)
Output[('CodingForAll', 'Java', 1200), ('CodeWars', 'Python', 1100)]
Using For Loop and remove() Function
In this method we use a for loop and the remove() function to remove unwanted tuples directly from the original list li.
Python
li = [
("GeeksforGeeks", "Python", 1000),
("GeeksforGeeks", "JavaScript", 1500),
("CodingForAll", "Java", 1200),
("GeeksforGeeks", "C++", 1300),
("CodeWars", "Python", 1100)
]
# iterating through the list
for tup in li[:]:
if tup[0] == "GeeksforGeeks":
li.remove(tup)
print(li)
Output[('CodingForAll', 'Java', 1200), ('CodeWars', 'Python', 1100)]
Similar Reads
Get first element from a List of tuples - Python The goal here is to extract the first element from each tuple in a list of tuples. For example, given a list [(1, 'sravan'), (2, 'ojaswi'), (3, 'bobby')], we want to retrieve [1, 2, 3], which represents the first element of each tuple. There are several ways to achieve this, each varying in terms of
2 min read
Python - Restrict Tuples by frequency of first element's value Given a Tuple list, the task is to write a Python program to restrict the frequency of the 1st element of tuple values to at most K. Examples: Input : test_list = [(2, 3), (3, 3), (1, 4), (2, 4), (2, 5), (3, 4), (1, 4), (3, 4), (4, 7)], K = 2 Output : [(2, 3), (3, 3), (1, 4), (2, 4), (3, 4), (1, 4),
3 min read
Python - Remove given character from first element of Tuple Given a Tuple list, remove K character from 1st element of the Tuple being String. Input : test_list = [("GF$g!", 5), ("!i$s", 4), ("best!$", 10)], K = '$' Output : [('GFg!', 5), ('!is', 4), ('best!', 10)] Explanation : First element's strings K value removed. Input : test_list = [("GF$g!", 5), ("be
5 min read
Remove an Element from a List by Index in Python We are given a list and an index value. We have to remove an element from a list that is present at that index value in a list in Python. In this article, we will see how we can remove an element from a list by using the index value in Python. Example: Input: [1, 2, 3, 4, 5], index = 2 Output: [1, 2
3 min read
Remove Unordered Duplicate Elements from a List - Python Given a list of elements, the task is to remove all duplicate elements from the list while maintaining the original order of the elements.For example, if the input is [1, 2, 2, 3, 1] the expected output is [1, 2, 3]. Let's explore various methods to achieve this in Python.Using setWe can initialize
3 min read