How to remove tuples in a list of tuples when the value of the first tuple in contained in an other list?
Question:
I have a list containing tuples and I would like to remove tuples that contain words in the first position of the tuple based on words from a second list.
list_of_tuples = [
("apple",2),
("banana",54),
("flower", 5),
("apple",4),
("fruit", 3)
]
list_of_words = [
"apple",
"banana"
]
The final result should look like this:
[("flower", 5), ("fruit", 3)]
Answers:
This code will do the trick:
list_of_tuples = [
("apple", 2),
("banana", 54),
("flower", 5),
("apple", 4),
("fruit", 3)
]
list_of_words = [
"apple",
"banana"
]
final_list_of_tuples = [tup for tup in list_of_tuples if tup[0] not in list_of_words]
print(final_list_of_tuples)
The one liner technique is called a list comprehension.
You can find more information about it here:
Rather than a complete solution, here is a breakdown of the various operations you can put together to accomplish your task. Hopefully it gives you a feel for the Python building blocks you can use for these types of problems in the future:
list_of_tuples = [
("apple",2),
("banana",54),
("flower", 5),
("apple",4),
("fruit", 3)
]
list_of_words = ["apple", "banana"]
# demonstrates tuple unpacking in Python
word, quantity = list_of_tuples[0]
print(word, quantity)
# demonstrates how to test against a collection
print(word in list_of_words)
# demonstrates how to iterate over a list of tuples and unpack
for word, quantity in list_of_tuples:
print(f"word: {fruit}, quantity: {quantity}")
# demonstrates how to create a new list from an existing list
new_list_of_tuples = []
for word, quantity in list_of_tuples:
if word != "flower":
new_list_of_tuples.append((word, quantity))
print(new_list_of_tuples)
Output:
apple 2
True
word: apple, quantity: 2
word: apple, quantity: 54
word: apple, quantity: 5
word: apple, quantity: 4
word: apple, quantity: 3
[('apple', 2), ('banana', 54), ('apple', 4), ('fruit', 3)]
I have a list containing tuples and I would like to remove tuples that contain words in the first position of the tuple based on words from a second list.
list_of_tuples = [
("apple",2),
("banana",54),
("flower", 5),
("apple",4),
("fruit", 3)
]
list_of_words = [
"apple",
"banana"
]
The final result should look like this:
[("flower", 5), ("fruit", 3)]
This code will do the trick:
list_of_tuples = [
("apple", 2),
("banana", 54),
("flower", 5),
("apple", 4),
("fruit", 3)
]
list_of_words = [
"apple",
"banana"
]
final_list_of_tuples = [tup for tup in list_of_tuples if tup[0] not in list_of_words]
print(final_list_of_tuples)
The one liner technique is called a list comprehension.
You can find more information about it here:
Rather than a complete solution, here is a breakdown of the various operations you can put together to accomplish your task. Hopefully it gives you a feel for the Python building blocks you can use for these types of problems in the future:
list_of_tuples = [
("apple",2),
("banana",54),
("flower", 5),
("apple",4),
("fruit", 3)
]
list_of_words = ["apple", "banana"]
# demonstrates tuple unpacking in Python
word, quantity = list_of_tuples[0]
print(word, quantity)
# demonstrates how to test against a collection
print(word in list_of_words)
# demonstrates how to iterate over a list of tuples and unpack
for word, quantity in list_of_tuples:
print(f"word: {fruit}, quantity: {quantity}")
# demonstrates how to create a new list from an existing list
new_list_of_tuples = []
for word, quantity in list_of_tuples:
if word != "flower":
new_list_of_tuples.append((word, quantity))
print(new_list_of_tuples)
Output:
apple 2
True
word: apple, quantity: 2
word: apple, quantity: 54
word: apple, quantity: 5
word: apple, quantity: 4
word: apple, quantity: 3
[('apple', 2), ('banana', 54), ('apple', 4), ('fruit', 3)]