w3resource logo
Python Exercises

Python Exercise: Convert a list into a nested dictionary of keys

Python dictionary: Exercise-27 with Solution

Write a Python program to convert a list into a nested dictionary of keys.

Sample Solution:-

Python Code:

num_list = [1, 2, 3, 4]
new_dict = current = {}
for name in num_list:
    current[name] = {}
    current = current[name]

Sample Output:

{1: {2: {3: {4: {}}}}}

Python Code Editor:

Have another way to solve this solution? Contribute youe code (and comments) through Disqus.

New exercises: Matplotlib: Exercises, Practice, Solution

ankara escort ankara escort ankara escort antalya escort alanya escort