I have a list of tuples like this:
mylist = [
('Apple', 'Pass'),
('Banana', 'Fail'),
('Orange', 'Pass'),
('Apple', 'Fail')
]
I want to remove tuple ('Apple', 'Fail')
as I have ('Apple', 'Pass')
, meaning remove tuple which has Fail
if another tuple has Pass
for Apple
and so on, so the new list looks like this:
newList = [
('Apple', 'Pass'),
('Banana', 'Fail'),
('Orange', 'Pass')
]
How would I do this?
>Solution :
Use:
# create a set to search for tuples that has Pass
pass_tuples = {e for e, s in mylist if s == 'Pass'}
# keep tuples that are Pass or doesn't have a Pass
newList = [(e, s) for e, s in mylist if s == 'Pass' or e not in pass_tuples]
print(newList)
Output
[('Apple', 'Pass'), ('Banana', 'Fail'), ('Orange', 'Pass')]
Basically you want to keep the elements in newList that are Pass
or that doesn’t have a Pass
.
Note: The time complexity of this approach is expected O(n)
.