In this python dictionary program, we will convert a list into a nested dictionary of keys.
Steps to solve the program
- Take a list of characters as input and create two empty dictionaries.
- Use for loop to iterate over characters in the list.
- Add the character as the key and an empty dictionary as its value.
- Now add the above key to the empty dictionary.
- Print the output.
list1 = ['a','b','c','d']
new_dict = current = {}
for char in list1:
current[char] = {}
current = current[char]
print(new_dict)
Output :
{'a': {'b': {'c': {'d': {}}}}}