user2368334
user2368334

Reputation: 73

How can I remove tuples containing a specific value from a list?

My list of tuples is:

[('Huggies', 0.39), ('Snugglers', 0.26), ('Pie', 0.23), ('Baby Love', 0.23)]

and i need to remove all tuples that do not contain 0.23. How can i do this?

Upvotes: 0

Views: 1427

Answers (1)

NPE
NPE

Reputation: 500327

To compare every element of the tuple:

>>> l = [('Huggies', 0.39), ('Snugglers', 0.26), ('Pie', 0.23), ('Baby Love', 0.23)]
>>> l = [t for t in l if 0.23 in t]
>>> l
[('Pie', 0.23), ('Baby Love', 0.23)]

To compare only the second element:

>>> l = [('Huggies', 0.39), ('Snugglers', 0.26), ('Pie', 0.23), ('Baby Love', 0.23)]
>>> l = [t for t in l if t[1] == 0.23]
>>> l
[('Pie', 0.23), ('Baby Love', 0.23)]

Upvotes: 4

Related Questions