Skip to content

Nested dictionary Python user input | Example code

  • by

Using for loop and range() function can create a Nested dictionary from user input in Python.

Example nested dictionary Python user input

Simple example code creates a nested dictionary with user input in python.

d = {}

size = int(input("Enter the size of nested dictionary: "))

for i in range(size):
    dict_name = input("Enter the name of child dictionary: ")

    d[dict_name] = {}
    Name = input("Enter name: ")
    Age = input("Enter Age: ")
    d[dict_name]["Name"] = Name
    d[dict_name]["Age"] = Age

print(d)

Output:

Nested dictionary Python user input

How can I make the user input key and value dictionary in Python?

e.g. classe_list = input {“name”: “score”}

name = input()
score = input()

class_list = {name: score}

print(class_list)

Output:

a
100
{‘a’: ‘100’}

Do comment if you have any doubts or suggestions on this Python input topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading