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?
CodePudding user response:
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)
.
CodePudding user response:
# your code goes here
mylist = [
('Apple', 'Pass'),
('Banana', 'Fail'),
('Orange', 'Pass'),
('Apple', 'Fail')
]
result = {}
for a, b in mylist:
if a not in result:
result[a] = b
else:
if result[a] =='Fail' and b=='Pass':
result[a] = b
mylist = list(result.items() )
print(mylist)
output
[('Apple', 'Pass'), ('Banana', 'Fail'), ('Orange', 'Pass')]
CodePudding user response:
#create 2 different sets for each fail and pass...
setForPass = set() setForFail = set()
for i in mylist:
if i[1] == 'Pass':
setForPass.add(i[0])
for i in mylist:
if i[1] == 'Fail':
if i[0] in setForPass:
mylist.remove(i)
output = [('Apple','Pass'), ('Banana','Fail'), ('Orange','Pass')]
CodePudding user response:
You can use the key-value pairs to build a dict, and use the max
function to overwrite the current value of a key if the new value is greater (since Pass
is greater than Fail
lexicographically; otherwise use a key function). Convert the dict items back to a list of tuples for output:
d = {}
for k, v in mylist:
d[k] = max(v, d.get(k, v))
newList = list(d.items())