How to Increment value in Dictionary in Python : Various Methods

Dictionary is a collection that is ordered, and changeable. It allows you to store unique values in it. There are many ways you can manipulate it. For example, change the value of the specific key in the dictionary. In this entire tutorial, you will learn how to increment the value in a dictionary in python.

How to create Dictionary in python?

Before explaining the ways to increment value in a dictionary, first learn how to create a dictionary in python. You can create a dictionary in python using the curly brackets “{}”. Inside it, you have to define the key and its value after the colon ( : ). Below is the syntax of it.

my_dic = { "key1":"value1","key2":"value2"}

You can create a duplicate key. But the last key-value pair will be used. For example, I have defined a country two times with different values. The python interpreter will use the last key value { UK}.

my_dict = {"country":"USA","dial_code":"+1","country":"UK"}
print(my_dict)

Output

{'country': 'UK', 'dial_code': '+1'}

Methods to Increment Value in Dictionary in Python

In this section, you will learn all the methods that you can apply to increment the value in Dictionary in python.

Method 1: Using loop in both key and value Pair

The first method to increment the value in the dictionary is the use of key and pair values in iterations. It will loop through each key and corresponding value and change the value.

Run the below lines of code to increment the value.

my_dict = {"key1":10,"key2":20,"key3":30}
for k ,v in my_dict.items():
    my_dict[k]= v+ 1
print(my_dict)

Output

Incrementing the value using key and pair in iteration
Incrementing the value using key and pair in iteration

Method 2:  Use dict comprehension

The second method to increment is the use of dict comprehension. In the above method, you have used for loop to iterate. Here you will combine everything in the for loop in a single statement using the curly bracket.

my_dict = {"key1":10,"key2":20,"key3":30}
{k:v+1 for k,v in my_dict.items()}
print(my_dict)

Output

{'key1': 11, 'key2': 21, 'key3': 31}

Method 3: Increment value in dictionary in python using keys

The third method to increment value is the use of the key in the dictionary. In method 1, you have taken key and value in each iteration. But in this method, you will iterate on dictionary keys only.

Execute the below lines of code and increment the value of the dictionary.

my_dict = {"key1":10,"key2":20,"key3":30}
for k in my_dict.keys():
    my_dict[k]= my_dict[k]+ 1
print(my_dict)

Output

Incrementing the value using key in iteration
Incrementing the value using key in iteration

Conclusion

There are various methods to increment the value of the corresponding key of the dictionary in python. The choice to choose the method depends upon you. I advise you to choose the one which fits your dictionary values and takes less time.

Anyway, you can contact us for more help on this topic.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link