naren_paste

nested_dicr

Aug 1st, 2024
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | Source Code | 0 0
  1. nested_dict = {
  2.     'a': {
  3.         'b': {
  4.             'c': 10,
  5.             'd': 20
  6.         },
  7.         'e': 30
  8.     },
  9.     'f': 40
  10. }
  11. keys = ['a', 'b', 'c']
  12.  
  13. for key in keys:
  14.         # Check if the current key exists in the current dictionary
  15.         if key in nested_dict:
  16.             # Update the current dictionary to the nested dictionary corresponding to the key
  17.             nested_dict = nested_dict[key]
  18.             print(nested_dict)
  19.         else:
  20.             # If the key does not exist, return None
  21.             print(None)
Advertisement
Add Comment
Please, Sign In to add comment