Python Dictionary: How to Create, Add, Replace, Retrieve, Remove

5/5 - (6 votes)

Python defines the dictionary data type as an unordered collection that contains key:value pairs. The key:value pairs (separated by commas) are wrapped inside curly braces ({}). Β 

Each key inside the dictionary must be unique and immutable. Dictionary keys can be one of the following data types: integer, string, or tuple. Dictionary keys can not be a list as lists are mutable. Values can be of any data type and do not need to be unique.

Python Dictionary – The Ultimate Guide

How to Create a Dictionary?

Python has two ways to create/initialize an empty dictionary object. One option is to use curly braces ({}). The other is to use the built-in dict() constructor method. For this tutorial, the curly braces({}) option will be referenced.

composers = {}
composers = dict()

You may want to create a dictionary and initialize it with key:value data in some instances. The example below is a small representation of classical composers and their respective birth years.

Dictionaries can handle various data types (outlined above) inside the same structure. For example, if we added a key (integer 2) and assigned this key a value (string 'test'), the dictionary would be updated with no errors. However, we recommend that for clarity, the initial structure design remains intact.

composers = {'Chopin':  1810,
             'Greeg':   1843,
             'Handel': 1684,
             'Mozart': 1756

How to Add a Key:Value Pair to a Dictionary?

To add a new key:value pair to a dictionary, add a key inside the square brackets at the end of the dictionary reference. Assign this key a value using the equals (=) sign. 

The code below adds the composer Bach and his birth year and displays the output.

composers['Bach'] = 1685


{'Chopin': 1810, 'Greeg': 1843, 'Handel': 1684, 
 'Mozart': 1756, 'Bach': 1685}

How to Replace a Key in a Dictionary?

Use the method to replace a key in an existing key:value pair. For example, this method updates an existing dictionary key with a new key.

In the dictionary composers created earlier, the spelling of Grieg (Greeg) contained a typing error. After running this code, the key reflects the correct name in the output below.

composers['Grieg'] = composers.pop('Greeg')


{'Chopin': 1810, 'Handel': 1684, 'Mozart': 1756, 
 'Bach': 1732, 'Grieg': 1843}

How to Replace a Value in a Dictionary?

To replace a value in an existing key:value pair, assign the key to be replaced inside the square brackets at the end of the dictionary reference. Then, assign a different value using the equals sign (=).

The code below changes the incorrect birth year for the composer Handel.

composers['Handel'] = 1685


{'Chopin': 1810, 'Handel': 1685, 'Mozart': 1756, 
 'Bach': 1732, 'Grieg': 1843}

How to Retrieve a Value associated with a Key?

To retrieve a value associated with a specific existing dictionary key, create a variable (x) and assign this variable to the appropriate key.  If the key does not exist, an error will occur. The value of x (the birth year of Chopin) is displayed.

x = composers['Chopin']



How to Remove a Key:Value Pair in a Dictionary?

To remove an unwanted dictionary key:value pair, use the pop() method. The parameter inside pop() must be an existing dictionary key, or an error will occur. The output displays the modified dictionary with the composer Mozart removed.



{'Chopin': 1810, 'Handel': 1685, 
 'Bach': 1732, 'Grieg': 1843}

To learn more about basic Python skills, feel free to check out our free cheat sheets and email academy: