简体   繁体   中英

Remove tuples within a list of tuples based on dictionary value within the tuple

I have a long list of tuples that contain a dictionary. I would like to remove tuples with a 'weight' value of 1.

List_example = [('WHT', 'WML, {'weight': 48}),
 ('WHHT','CCH', {'weight': 53}),
 ('WRT','KUF', {'weight': 1}),
 ('WHHT','RWH',{'weight': 1}))]

Desired output:

List_example = [('WHT', 'WML, {'weight': 48}),
 ('WHHT','CCH', {'weight': 53}))]

below

lst = [('WHT', 'WML', {'weight': 48}),
 ('WHHT','CCH', {'weight': 53}),
 ('WRT','KUF', {'weight': 1}),
 ('WHHT','RWH',{'weight': 1})]
 
new_lst = [x for x in lst if x[2]['weight'] != 1]
print(new_lst)

output

[('WHT', 'WML', {'weight': 48}), ('WHHT', 'CCH', {'weight': 53})]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM