Understanding Python Dictionaries With Simple Examples

In Python, lists work well for ordered items, but sometimes order is not the main concern. Sometimes you need fast access to values based on a label or a name. This is where dictionaries are useful. A dictionary stores data as pairs, linking a key to a value.

Each key acts like a unique label. Each value is the data tied to that label. Instead of looking up data by position, you look it up by name. This makes dictionaries feel more natural when structuring information.

You create a dictionary using curly braces. Keys and values are separated by colons.

Basic Python dictionary syntax showing the creation of a person dictionary with keys name, age, and city.


Every piece of data here is simple and clear. You can see what value each position represents, eliminating any uncertainty.

NOTE:You don’t have to assign the dictionary to a variable, but it’s a common practice to do so in order to keep it accessible for later use in your code.


Another option for creating a dictionary is to use the dict() constructor. This constructor creates a dictionary from a list of key-value pairs.

To create a dictionary, we pass a list of tuples to the dict() constructor. Each tuple consists of a key as the first element and its corresponding value as the second element.

Python code creating a dictionary called cheese using the dict() constructor with key–value pairs for name, price, calories per slice, and country.

When to Use Dictionaries

Dictionaries work best when data is identified by labels rather than positions. Settings, profiles, and records all fit this pattern well. Because searches are fast and direct, dictionaries are a common choice for organizing information cleanly.

Accessing and Updating Data

To retrieve a value, you use its key. We call this bracket notation:

Python code showing a dictionary named person with keys name, age, and city, and a print(person[


Dictionaries are flexible. You can change existing values or add new ones at any time.

Python example where the age value in the person dictionary is updated to 31 using person[


This ability makes dictionaries  particularly useful for handling data that changes during the program’s execution.

Dictionaries have methods that help you inspect their contents. You can loop through keys, values, or both.

Python code looping through a dictionary with person.items() and printing each key–value pair, producing output like name : Anna, age : 30, city : Rome.


The methods .keys() and .values() return a dictionary view that includes all the keys and values found in the dictionary.

Python example printing person.keys() and person.values(), with output displaying the list of dictionary keys and the list of corresponding values.


The .get() method helps you retrieve the value for a given key. While it resembles the bracket notation we talked about earlier, its main benefit is that you can provide a default value, so you won’t run into an error if the key isn’t present.

It’s syntax is: dictionary.get(key, default).

Python example using person.get() to retrieve values from a dictionary, showing a default value being returned for a missing key (

Wrapping It Up

Dictionaries create a balance between structure and flexibility. They map names to values and keep code readable. For beginners, they introduce a new way to think about data. Instead of asking where something is (index), you ask what it is (key). That shift makes programs easier to design and easier to understand.

Although my blog doesn’t support comments, feel free to reply via email or X.