How to add to dictionary in Python – with example

  • Reading time:15 mins read

How to add to dictionary in Python

A dictionary is an ordered collection of items stored in key/value pairs. To create a dictionary simply enclose a pair of items in curly braces with the first item being the key and the second item its respective value. Key/value pairs are separated by a full colon.

 

 

While values can be of any type, dictionary keys should strictly be of immutable types such as string. We can also create a dictionary using the built-in dict() function.

 

python add to dictionary

 

In this article, we’ll show you how to add to dictionary in Python, by creating a simple dictionary that is called product.

Using this dictionary we will explore how to access and add new elements to a dictionary using various methods.

 

How to add to dictionary in Python – Access items in a dictionary

Dictionaries are changeable which means we can add, change, and also remove items as we wish.

To access items in a dictionary we can use square brackets and the name of the key whose value we want to retrieve. For example, we can access the first item in the dictionary as shown here.

 

How to add to dictionary in Python – new key-value pairs

We can also add a new key/value pair to the dictionary, we can do this by calling the dictionary name followed by square brackets.

Inside the square brackets pass in the name of the key and then using the assignment operator assign it to its value.

 

 

As shown in the output the new key/value pair has been successfully added to the dictionary.

Create a blank dictionary

Alternatively, we can also create a blank dictionary and then go ahead to insert the key/value pairs as we go.

 

python add to dictionary

 

So we will start off by creating a blank dictionary, and now using square brackets and the assignment operator we can go ahead to insert the keys and their respective values as we go.

 

 

Prior to Python 3.6 dictionaries were considered to be unordered, however, in subsequent versions, dictionaries are considered to be ordered.

Indexing the keys

Irrespective of this we can simply access the values of dictionaries by indexing their respective key as we had done earlier. For example, in this new dictionary, we can also access the last key/value pair of price as shown below.

 

How to add to dictionary in Python – Data type variations

Keys can only be strings but values can be any other data type. For instance, we can remove the price key/value pair and instead have the product as the key and its value a list of various products.

 

python add to dictionary

 

Therefore, it’s worth noting that values unlike keys can be anything from strings, numbers, lists or even other dictionaries.

 

 

Summary

This is how to add to dictionary in Python. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.