map1 = { 1: "Sam", 2: "Rich", 3 : "Mike", 4: "Bob", 6: "Donald"}
map2 = { 5: "Sally", 6 : "Donald", 7: "Roger", 1: "Sam"}
I want to print common names in both dicts, Can anyone help me out
You can recover your values with dict.values
and cast them to sets. This allows you to find their intersection.
print(set(map1.values()) & set(map2.values()))
Try this using set
map1 = { 1: "Sam", 2: "Rich", 3 : "Mike", 4: "Bob", 6: "Donald"}
map2 = { 5: "Sally", 6 : "Donald", 7: "Roger", 1: "Sam"}
list(set(map1.values()).intersection(set(map2.values())))
it returns list with common values in both dictionary
set(map1.values()).intersection(set(map2.values()))
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.