Given two dictionaries say d1 and d2. Write a program that list the overlapping keys of the dictionaries,i.E if a key of d1 is aloso a key of d2, the list it
Answers
Program: (LANGUAGE : Python 3)
d1 = {'nice': 1, 'to' : 2 , 'meet' : 3, 'you' : 4} #Sample dictionary 1
d2 = {'you' : 'x' ,'are' : 'y', 'great' :'z'} #Sample dictionary 2
Common_Keys =[ ] #Empty list
for i in d1.keys() :
if i in d2.keys(): #Comparing Keys from d1 and d2
Common_Keys.append(i)
#Adding common keys to the list.
print( Common_Keys )
Output:
['you']
The above program has 2 fixed dictionaries. However you can input your own dictionaries by the method given below.
METHOD 2:
d1 = eval(input("Enter first dictionary: "))
d2 = eval(input("Enter second dictionary: "))
Common_Keys =[ ] #Empty list
for i in d1.keys() :
if i in d2.keys(): #Comparing Keys from d1 and d2
Common_Keys.append(i)
#Adding common keys to the list.
print( Common_Keys )
METHOD 3:
We can also create two lists containing keys of dictionary 1 and 2 respectively and then search for common keys .
d1 = eval(input("Enter first dictionary: "))
d2 = eval(input("Enter second dictionary: "))
lst1 = (d1.keys() ) #List of keys of d1
lst2 = (d2.keys() ) #List of keys of d2
lst = [ ] #Empty list
for i in lst1 :
for j in lst2 :
if i == j :
lst += [ i ]
print("Common keys is/are :", lst)
Hope this answer helps.
Python program that list the overlapping keys of the dictionaries.
Program :
print("Dynamic \n")
dups1=[]
d1=eval(input("Enter first dictinary : "))
d2=eval(input("Enter second dictionary : "))
for i in d1.keys():
if i in d2.keys():
dups1.append(i)
print("\nduplicate key is/are : ",dups1)
print("\n \n \nStatic \n")
dups2=[]
d3={1: 'apple', 2: 'ball', 3:'cat'}
d4={3: 'lol', 4:'rofl', 1:'hmm', 8:'gn'}
print("First dictionary taken statically is : ",d3)
print("Second dictionary taken statically is : ",d4)
for i in d3.keys():
if i in d4.keys():
dups2.append(i)
print("\nduplicate key is/are : ",dups2)
Input : (For dynamic part)
Enter first dictinary : {'hi':1, 'hello':2, 'bye':9}
Enter second dictionary : {'how':'I', 'are':'am', 'you?':'fine', 'bye':'bye'}
Output :
Output for the dynamically taken input :
Dynamic
duplicate key is/are : ['bye']
Output for the statically taken input :
Static
First dictionary taken statically is : {1: 'apple', 2: 'ball', 3: 'cat'}
Second dictionary taken statically is : {3: 'lol', 4: 'rofl', 1: 'hmm', 8: 'gn'}
duplicate key is/are : [1, 3]
Explanation :
For the first part of code, we need to take input and the function eval() will evaluate the input() provided by the user as, dictionary, if user gives the correct input. Then we write a loop taking the keys of first dictionary and an if statement within that sees if the keyvariable's value is present in the second list too or not. If yes, it adds the key to duplicate list, else not.
Similarly, we write the static code too. The only difference is, we will directly take the input in the code itself.
Questions that may help you to understand this concept a bit more :
- Complete basic knowlege of dictionaries is available in this link. Have a look!
https://brainly.in/question/14673591
- Advantages of Programming in python?
brainly.in/question/11007952
Hope it helps! Thank you!