Moe Siddig Moe Siddig - 4 months ago 9
Python Question

Replace list value in Python dictionary

I have the following dictionary. For objects that have the value

['FAIL', 'PASS']
, I'd like to replace it with
'PASS'
only.

Dict = [('Main Menu-046', ['PASS']), ('Main Menu-047', ['FAIL']), ('Main Menu-044', ['FAIL', 'PASS']), ('Main Menu-045', ['PASS']), ('Main Menu-042', ['FAIL', 'PASS']), ('Main Menu-043', ['FAIL'])


I tried the following:

if "FAIL" in [x for v in Dict.values() for x in v]: ## if true
Dict.values == "PASS"

Answer

If I understood your question correctly, you want to replace ['FAIL', 'PASS'] with ['PASS']

processed_list = [(x[0], ["PASS"] if "PASS" in x[1] else x[1]) for x in Dict]

Results in:

[('Main Menu-046', ['PASS']),
 ('Main Menu-047', ['FAIL']),
 ('Main Menu-044', ['PASS']),
 ('Main Menu-045', ['PASS']),
 ('Main Menu-042', ['PASS']),
 ('Main Menu-043', ['FAIL'])]