How to return a boolean to check if elements in two dicts are the same in python dictionaries?
Question:
I have two dictionaries:
dict1 = {
'Argentina': ['Buenos Aires'],
'Bangladesh': ['Dhaka'],
'Brazil': ['São Paulo', 'Rio de Janeiro']
dict2 = {
1392685764: 'Tokyo', 1356226629: 'Mumbai', 1156073548: 'Shanghai',
1484247881: 'Mexico City', 1156237133: 'Guangzhou', 1818253931: 'Cairo',
1156228865: 'Beijing', 1840034016: 'New York', 1643318494: 'Moscow',
1764068610: 'Bangkok', 1050529279: 'Dhaka', 1032717330: 'Buenos Aires'}
I would like to check that the nested values in dict1 have any elements in common with values in dict2. I’ve been doing this source:
def f(d1, d2, id, country):
return set(d1.values()) == set(d2.values())
So, when the function is called with arguments dict1, dict2, country=Argentina and id=1032717330 returns True.
But the result is always an TypeError. Any help will be appreciate.
Answers:
It seems that dict1
has lists as values, so you need to "chain" them into a single list first. You cat try something like this:
from itertools import chain
set(chain(*dict1.values())) == set(dict2.values())
It’s unclear how you want to use id and country, so I’ve left this out of the function.
Both dictionaries and sets are unordered which is probably why you’re getting different results. Assuming Python, you could do something like this:
def has_same_values(d1, d2):
values1 = []
values2 = []
for key in d1.keys():
values1.extend(d1[key])
for key in d2.keys():
values2.extend(d2[key])
return values1.sort() == values2.sort()
You need to convert the first dictionary values makes flatern,
def f(d1, d2):
'''d1 values should be list'''
return set([i for sublist in d1.values() for i in sublist]) == set(d2.values())
Execution:
In [1]: f(dict1, dict2)
Out[1]: False
EDIT
On your reversed requirements, you have to do this. You need to check the d2.value
is exists in d1.value
def f(d1, d2, id, country):
return d2.get(id) in d1.get(country)
f(dict1, dict2, 1032717330, 'Argentina')
# True
I have two dictionaries:
dict1 = {
'Argentina': ['Buenos Aires'],
'Bangladesh': ['Dhaka'],
'Brazil': ['São Paulo', 'Rio de Janeiro']
dict2 = {
1392685764: 'Tokyo', 1356226629: 'Mumbai', 1156073548: 'Shanghai',
1484247881: 'Mexico City', 1156237133: 'Guangzhou', 1818253931: 'Cairo',
1156228865: 'Beijing', 1840034016: 'New York', 1643318494: 'Moscow',
1764068610: 'Bangkok', 1050529279: 'Dhaka', 1032717330: 'Buenos Aires'}
I would like to check that the nested values in dict1 have any elements in common with values in dict2. I’ve been doing this source:
def f(d1, d2, id, country):
return set(d1.values()) == set(d2.values())
So, when the function is called with arguments dict1, dict2, country=Argentina and id=1032717330 returns True.
But the result is always an TypeError. Any help will be appreciate.
It seems that dict1
has lists as values, so you need to "chain" them into a single list first. You cat try something like this:
from itertools import chain
set(chain(*dict1.values())) == set(dict2.values())
It’s unclear how you want to use id and country, so I’ve left this out of the function.
Both dictionaries and sets are unordered which is probably why you’re getting different results. Assuming Python, you could do something like this:
def has_same_values(d1, d2):
values1 = []
values2 = []
for key in d1.keys():
values1.extend(d1[key])
for key in d2.keys():
values2.extend(d2[key])
return values1.sort() == values2.sort()
You need to convert the first dictionary values makes flatern,
def f(d1, d2):
'''d1 values should be list'''
return set([i for sublist in d1.values() for i in sublist]) == set(d2.values())
Execution:
In [1]: f(dict1, dict2)
Out[1]: False
EDIT
On your reversed requirements, you have to do this. You need to check the d2.value
is exists in d1.value
def f(d1, d2, id, country):
return d2.get(id) in d1.get(country)
f(dict1, dict2, 1032717330, 'Argentina')
# True