Advertisement
Blessing988

Untitled

Mar 8th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. #my function
  2.  
  3. def common_elements(a_dict):
  4.  
  5.     empty_list = [  ]
  6.  
  7.     for key, value in a_dict.items( ):
  8.  
  9.         if key in a_dict.keys( ) and key in a_dict.values( ):   #if an element is both a value and key
  10.  
  11.             empty_list.append(key)
  12.  
  13.     print(empty_list)
  14.  
  15.  
  16. my_dict = {
  17.     "A": "K",
  18.     "B": "D",
  19.     "C": "A",
  20.     "D": "Z"
  21. }                                   #Sample dictionary
  22.  
  23. common_elements(my_dict)                                                                                      #This would return A and D because both A and D are values and keys in my_dict dictionary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement