Reputation: 73
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
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