Open In App

How to add values to dictionary in Python

Last Updated : 27 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn what are the different ways to add values in a dictionary in Python

Assign values using unique keys

After defining a dictionary we can index through it using a key and assign a value to it to make a key-value pair.

Python3




veg_dict = {}
veg_dict[0] = 'Carrot'
veg_dict[1] = 'Raddish'
veg_dict[2] = 'Brinjal'
veg_dict[3] = 'Potato'
 
print(veg_dict)


Output:

{0: ‘Carrot’, 1: ‘Raddish’, 2: ‘Brinjal’, 3: ‘Potato’}

But what if a key already exists in it?

Python3




veg_dict[0] = 'Tomato'
print(veg_dict)


Output:

{0: ‘Tomato’, 1: ‘Raddish’, 2: ‘Brinjal’, 3: ‘Potato’}

We can observe that the value corresponding to key 0 has been updated to ‘Tomato’ from ‘Carrot’.

Merging two dictionaries using update()

We can merge two dictionaries by using the update() function.

Python3




veg_dict = {0: 'Carrot', 1: 'Raddish',
            2: 'Brinjal', 3: 'Potato'}
 
veg_dict.update({4: 'Tomato', 5: 'Spinach'})
print(veg_dict)


Output:

{0: ‘Carrot’, 1: ‘Raddish’, 2: ‘Brinjal’, 3: ‘Potato’, 4: ‘Tomato’, 5: ‘Spinach’}

Add values to dictionary Using two lists of the same length

This method is used if we have two lists and we want to convert them into a dictionary with one being key and the other one as corresponding values.

Python3




roll_no = [10, 20, 30, 40, 50]
names = ['Ramesh', 'Mahesh', 'Kamlesh',
         'Suresh', 'Dinesh']
 
students = dict(zip(roll_no, names))
print(students)


Output:

{10: ‘Ramesh’, 20: ‘Mahesh’, 30: ‘Kamlesh’, 40: ‘Suresh’, 50: ‘Dinesh’}

Converting a list to the dictionary

This method is used to convert a python list into a python dictionary with indexes as keys.

Python3




vegetables = ['Carrot', 'Raddish',
              'Brinjal', 'Potato']
 
veg_dict = dict(enumerate(vegetables))
print(veg_dict)


Output:

{0: ‘Carrot’, 1: ‘Raddish’, 2: ‘Brinjal’, 3: ‘Potato’}

There are other methods as well to add values to a dictionary but the above-shown methods are some of the most commonly used ones and highly efficient as well.

Add values to dictionary Using the merge( | ) operator

By using the merge operator we can combine two dictionaries.

Python3




veg1 = {0: 'Carrot', 1: 'Raddish'}
veg2 = {2: 'Brinjal', 3: 'Potato'}
 
veg_dict = (veg1 | veg2)
print(veg_dict)


Output:

{0: ‘Carrot’, 1: ‘Raddish’, 2: ‘Brinjal’, 3: ‘Potato’}

Add values to dictionary Using the in-place merge( |= ) operator.

By using the in place merge operator we can combine two dictionaries. In in place operator, all elements of one dictionary are added in the other but while using merge original state of the dictionaries remains the same and a new one is created.

Python3




veg1 = {0:'Carrot', 1:'Raddish'}
veg2 = {2:'Brinjal', 3:'Potato'}
 
veg1 |= veg2
print(veg1)


Output:

{0: ‘Carrot’, 1: ‘Raddish’, 2: ‘Brinjal’, 3: ‘Potato’}

Note: Methods 4 and 5 work in only Python 3.9+ versions.



Previous Article
Next Article

Similar Reads

Python - Append Dictionary Keys and Values ( In order ) in dictionary
Given a dictionary, perform append of keys followed by values in list. Input : test_dict = {"Gfg" : 1, "is" : 2, "Best" : 3} Output : ['Gfg', 'is', 'Best', 1, 2, 3] Explanation : All the keys before all the values in list. Input : test_dict = {"Gfg" : 1, "Best" : 3} Output : ['Gfg', 'Best', 1, 3] Explanation : All the keys before all the values in
5 min read
Python program to update a dictionary with the values from a dictionary list
Given a dictionary and dictionary list, update the dictionary with dictionary list values. Input : test_dict = {"Gfg" : 2, "is" : 1, "Best" : 3}, dict_list = [{'for' : 3, 'all' : 7}, {'and' : 1, 'CS' : 9}] Output : {'Gfg': 2, 'is': 1, 'Best': 3, 'for': 3, 'all': 7, 'and': 1, 'CS': 9} Explanation : All dictionary keys updated in single dictionary. I
8 min read
Python - Filter dictionary values in heterogeneous dictionary
Sometimes, while working with Python dictionaries, we can have a problem in which we need to filter out certain values based on certain conditions on a particular type, e.g all values smaller than K. This task becomes complex when dictionary values can be heterogeneous. This kind of problem can have applications across many domains. Let's discuss c
6 min read
Python - Split Dictionary values on size limit of values
Given a dictionary with string values, the task is to write a python program to split values if the size of string exceeds K. Input : {1 : "Geeksforgeeks", 2 : "best for", 3 : "all geeks"}, limit = 5Output : {1: 'Geeks', 2: 'forge', 3: 'eks', 4: 'best ', 5: 'for', 6: 'all g', 7: 'eeks'}Explanation : All string values are capped till length 5. New v
8 min read
Python - Extract Unique values dictionary values
Sometimes, while working with data, we can have problem in which we need to perform the extraction of only unique values from dictionary values list. This can have application in many domains such as web development. Lets discuss certain ways in which this task can be performed. Extract Unique values dictionary values Using sorted() + set comprehen
7 min read
Python - Remove duplicate values across Dictionary Values
Sometimes, while working with Python dictionaries, we can have a problem in which we need to remove all the duplicate values across all the dictionary value lists. This problem can have applications in data domains and web development domains. Let's discuss certain ways in which this task can be performed. Input: test_dict = {'Manjeet': [1], 'Akash
8 min read
Python - Test for Even values dictionary values lists
Given a dictionary with lists as values, map Boolean values depending upon all values in List are Even or not. Input : {"Gfg" : [6, 8, 10], "is" : [8, 10, 12, 16], "Best" : [10, 16, 14, 6]} Output : {'Gfg': True, 'is': True, 'Best': True} Explanation : All lists have even numbers. Input : {"Gfg" : [6, 5, 10], "is" : [8, 10, 11, 16], "Best" : [10, 1
8 min read
Python - Add values to Dictionary of List
In this article, we will discuss how to add values to the dictionary of lists. We can add values to a dictionary by using a list, the input list would be the following structure: [('key',value),...........,('key',value)] So the above list is an input, which we can use with for loop to add values to the dictionary of lists. Syntax: for key, value in
2 min read
Different ways of sorting Dictionary by Values and Reverse sorting by values
Prerequisite: Dictionaries in Python A dictionary is a collection which is unordered, changeable, and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. We can access the values of the dictionary using keys. In this article, 10 different ways of sorting the Python dictionary by values and also reverse s
15+ min read
Python | Convert flattened dictionary into nested dictionary
Given a flattened dictionary, the task is to convert that dictionary into a nested dictionary where keys are needed to be split at '_' considering where nested dictionary will be started. Method #1: Using Naive Approach Step-by-step approach : Define a function named insert that takes two parameters, a dictionary (dct) and a list (lst). This functi
8 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg