Open In App

How To Add User Input To A Dictionary In Python

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, a dictionary is a built-in data type that represents an unordered collection of key-value pairs. Dictionaries are sometimes referred to as “dicts.” They provide a way to store and retrieve data efficiently based on keys. Dictionaries in Python are defined using curly braces {}. In this article, we will add user input to a dictionary in Python.

Add User Input to a Dictionary in Python

Below are some of the ways by which we can add user input to a dictionary in Python:

  • Using a Loop with input() Function
  • Using Dictionary Comprehension with input() Function
  • Using update() Method

Using a Loop with input() Function

In this example, the user is prompted to input the number of dictionary entries they want to add. A for loop iterates through the specified number, collecting key-value pairs from the user and updating a dictionary (`user_dict`) with the input. Finally, the resulting dictionary is printed, reflecting the added user input.

Python3




user_dict = {}
 
num_entries = int(input("Enter the number of entries you want to add: "))
 
for i in range(num_entries):
    key = input("Enter key: ")
    value = input("Enter value: ")
    user_dict[key] = value
 
print("Dictionary after adding user input:", user_dict)


Output:

Enter the number of entries you want to add: 4
Enter key: adarsh
Enter value: 12
Enter key: raj
Enter value: 10
Enter key: Aditya
Enter value: 10
Enter key: Anish
Enter value: 11
Dictionary after adding user input: {'adarsh': '12', 'raj': '10', 'Aditya': '10', 'Anish': '11'}

Using Dictionary Comprehension with input() Function

In this example, the user is prompted to input the number of dictionary entries they want to add. Using a dictionary comprehension, the program then collects key-value pairs from the user in a concise manner. The resulting dictionary (`user_dict`) is printed, reflecting the added user input.

Python3




num_entries = int(input("Enter the number of entries you want to add: "))
 
user_dict = {input(f"Enter key {i+1}: "): input(f"Enter value {i+1}: ") for i in range(num_entries)}
 
print("Dictionary after adding user input:", user_dict)


Output:

Enter the number of entries you want to add: 2
Enter key 1: Adarsh
Enter value 1: 12
Enter key 2: Raj
Enter value 2: 10
Dictionary after adding user input: {'Adarsh': '12', 'Raj': '10'}

Using update() Method

In this example, the user is prompted to input the number of dictionary entries they want to add. Through a for loop, key-value pairs are collected from the user, and the `update()` method is used to add these pairs to the existing dictionary (`user_dict`). Finally, the resulting dictionary is printed, reflecting the added user input.

Python3




user_dict = {}
 
num_entries = int(input("Enter the number of entries you want to add: "))
 
for i in range(num_entries):
    key = input("Enter key: ")
    value = input("Enter value: ")
    user_dict.update({key: value})
 
print("Dictionary after adding user input:", user_dict)


Output:

Enter the number of entries you want to add: 2
Enter key: Ram
Enter value: 11
Enter key: raj
Enter value: 122
Dictionary after adding user input: {'Ram': '11', 'raj': '122'}


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads