Reputation: 69
I want to compare two dictionary keys in python and if the keys are equal, then print their values.
For example,
dict_one={'12':'fariborz','13':'peter','14':'jadi'}
dict_two={'15':'ronaldo','16':'messi','12':'daei','14':'jafar'}
and after comparing the keys, print
'fariborz', 'daei'
'jadi', jafar'
Upvotes: 3
Views: 13898
Reputation: 3107
Using intersection .then you can get the same key value from dict_one
and dict_two
This is my code:
dict_one={'12':'fariborz','13':'peter','14':'jadi'}
dict_two={'15':'ronaldo','16':'messi','12':'daei','14':'jafar'}
print([(dict_one[vals],dict_two[vals]) for vals in dict_one.keys() & dict_two.keys()])
Output
[('fariborz', 'daei'), ('jadi', 'jafar')]
Upvotes: 0
Reputation: 4606
You can use the &
operator with to find the matching keys
for i in d1.keys() & d2.keys():
print("'{}', '{}'".format(d1[i], d2[i]))
~/python/stack/sept/twenty_2$ python3.7 alice.py 'fariborz', 'daei' 'jadi', 'jafar
Upvotes: 2
Reputation: 428
You're asking for the intersection of the two dictionaries.
set
You can use the builtin type set
for this, which implements the intersection()
function.
You can turn a list into a set like this:
set(my_list)
So, in order to find the intersection between the keys of two dictionaries, you can turn the keys into sets and find the intersection.
To get a list with the keys of a dictionary:
dict_one.keys()
So, to find the intersection between the keys of two dicts:
set(dict_one.keys()).intersection(set(dict_two.keys()))
This will return, in your example, the set {'12', '14'}
.
The code above in a more readable way:
keys_one = set(dict_one.keys())
keys_two = set(dict_one.keys())
same_keys = keys_one.intersection(keys_two)
# To get a list of the keys:
result = list(same_keys)
Another easy way to solve this problem would be using lambda functions.
I'm including this here just in case you'd like to know. Probably not the most efficient way to do!
same_keys = lambda first,second: [key1 for key1 in first.keys() if key1 in second.keys()]
So, as to get the result:
result = same_keys(dict_one,dict_two)
Any of the above two methods will give you the keys that are common to both dictionaries.
Just loop over it and do as you please to print the values:
for key in result:
print('{},{}'.format(dict_one[key], dict_two[key]))
Upvotes: 4
Reputation: 2845
for key, val1 in dict_one.items():
val2 = dict_two.get(key)
if val2 is not None:
print(val1, val2)
Upvotes: 0
Reputation: 318
def compare(dict1,dict2):
keys1 = dict1.keys()
keys2 = dict2.keys()
for key in keys1:
if key in keys:
print(dict1[key],dict2[key])
Upvotes: 0
Reputation: 1714
Dictionaries are nice in python because they allow us to look up a key's value very easily and also check if a key exists in the dict.
So in your example if you want to print the values for whenever the keys are the same between the dicts you can do something like this:
dict_one={'12':'fariborz','13':'peter','14':'jadi'}
dict_two={'15':'ronaldo','16':'messi','12':'daei','14':'jafar'}
# Loop through all keys in dict_one
for key in dict_one:
# Then check if each key exists in dict_two
if key in dict_two:
val1 = dict_one[key]
val2 = dict_two[key]
print('{},{}'.format(val1, val2)) # print out values with comma between them
Upvotes: 0
Reputation: 26039
Take iteration through one dictionary and check for existence of key in the other:
dict_one = {'12':'fariborz','13':'peter','14':'jadi'}
dict_two = {'15':'ronaldo','16':'messi','12':'daei','14':'jafar'}
for k in dict_one:
if k in dict_two:
print(dict_one[k], dict_two[k])
# fariborz daei
# jadi jafar
Upvotes: 0
Reputation: 106455
You can iterate over the intersection of the keys of the two dicts, and print the corresponding values of the two dicts after mapping them to the repr
function, which would help quote the strings:
for k in dict_one.keys() & dict_two.keys():
print(','.join(map(repr, (dict_one[k], dict_two[k]))))
This outputs:
'fariborz','daei'
'jadi','jafar'
Upvotes: 4