Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- nested_dict = {
- 'a': {
- 'b': {
- 'c': 10,
- 'd': 20
- },
- 'e': 30
- },
- 'f': 40
- }
- keys = ['a', 'b', 'c']
- for key in keys:
- # Check if the current key exists in the current dictionary
- if key in nested_dict:
- # Update the current dictionary to the nested dictionary corresponding to the key
- nested_dict = nested_dict[key]
- print(nested_dict)
- else:
- # If the key does not exist, return None
- print(None)
Advertisement
Add Comment
Please, Sign In to add comment