Open In App

How to Add User Input To A Dictionary - Python

Last Updated : 25 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of adding user input to a dictionary in Python involves taking dynamic data from the user and storing it in a dictionary as key-value pairs. Since dictionaries preserve the order of insertion, we can easily add new entries based on user input.

For instance, if a user inputs "name" as the key and "John" as the value, we can directly assign "name": "John" to the dictionary, building it incrementally with each user input.

Using dictionary comprehension

Dictionary comprehension is a efficient way to populate a dictionary in a single step. By combining iteration and input collection in one line, this method minimizes the code required and makes it highly readable. It is ideal when we want to create a dictionary quickly from user input.

Python
n = int(input("Enter the number of entries: "))
d = {input("Enter key: "): input("Enter value: ") for _ in range(n)}

print(d)

Output

Enter the number of entries: 3 
Enter key: Aditya
Enter value: 21
Enter key: Anish
Enter value: 32
Enter key: Arjun
Enter value: 10
{'Aditya': '21', 'Anish': '32', 'Arjun': '10'}

Explanation: This code takes an integer n as the number of entries, collects n key-value pairs and creates the dictionary d.

Using a list of tuples

In this method, key-value pairs are first collected as a list of tuples and then converted into a dictionary using dict() . This approach provides a clean separation between data collection and dictionary creation and making it particularly useful when dealing with a large number of entries .

Python
n = int(input("Enter the number of entries: "))
entries = [(input("Enter key: "), input("Enter value: ")) for _ in range(n)]
d = dict(entries)

print(d)

Output

Enter the number of entries: 3
Enter key: Aditya
Enter value: 21
Enter key: Anish
Enter value: 32
Enter key: Arjun
Enter value: 10
{'Aditya': '21', 'Anish': '32', 'Arjun': '10'}

Explanation: This code takes an integer n as the number of entries, collects n key-value pairs as tuples then converts the list of tuples into a dictionary d using dict() .

Using update()

update() allows us to add or modify entries in an existing dictionary. By iterating through user input in a loop, this method incrementally updates the dictionary with new key-value pairs. It’s particularly helpful when working with dictionaries that are need to be modified.

Python
d = {} # initializes an empty dictionary
n = int(input("Enter the number of entries: "))

for _ in range(n):
    key = input("Enter key: ")
    value = input("Enter value: ")
    d.update({key: value})

print(d)

Output

Enter the number of entries: 3
Enter key: Aditya
Enter value: 21
Enter key: Anish
Enter value: 32
Enter key: Arjun
Enter value: 10
{'Aditya': '21', 'Anish': '32', 'Arjun': '10'}

Explanation: This code takes an integer n as the number of entries then iterates n times to collect user inputs for keys and values, updating the dictionary d with each key-value pair using update().

Using setdefault()

setdefault() ensures that keys are added to a dictionary with default values if they don’t already exist. While this method is often used to handle default values, it can also be adapted for adding user input to a dictionary.

Python
d = {} # initializes an empty dictionary
n = int(input("Enter the number of entries: "))

for _ in range(n):
    key = input("Enter key: ")
    value = input("Enter value: ")
    d.setdefault(key, value)

print(d)

Output

Enter the number of entries: 3
Enter key: Aditya
Enter value: 21
Enter key: Anish
Enter value: 32
Enter key: Arjun
Enter value: 10
{'Aditya': '21', 'Anish': '32', 'Arjun': '10'}

Explanation: This code takes an integer n as the number of entries then iterates n times to collect user inputs for keys and values, adding each key-value pair to the dictionary d using setdefault() ensuring that the key is only added if it doesn't already exist.


Next Article
Article Tags :
Practice Tags :

Similar Reads