Python dictionaries for beginners

Once you understand lists, the next major step in Python is learning how to store data in a more structured way. Lists are excellent when order matters and you want to access items by position. But in many real programs, position is not the most natural way to find information. If you store a person’s name, age, and city, it is usually more useful to access those values by meaningful labels rather than by remembering whether the city is in position 2 or 3. This is exactly where Python dictionaries become important.

A dictionary stores data as key-value pairs. Instead of using numeric indexes like a list, a dictionary lets you use descriptive keys. That makes code easier to read and often more practical for real-world data. Dictionaries are widely used in Python for settings, user profiles, product details, API responses, configuration values, counters, grouped results, and many other tasks.

This lesson explains Python dictionaries for beginners in a clear, practical way. You will learn what a dictionary is, how to create one, how keys and values work, how to add, change, and remove entries, how to loop through a dictionary, what common beginner mistakes look like, and why dictionaries are so useful in real Python programming.

What is a dictionary in Python

A Python dictionary is a collection of key-value pairs.

Example:

person = {
“name”: “Alice”,
“age”: 25,
“city”: “Budapest”
}

In this dictionary:

  • "name" is a key, and "Alice" is its value
  • "age" is a key, and 25 is its value
  • "city" is a key, and "Budapest" is its value

This means the dictionary stores labeled pieces of data. Instead of saying “give me item number 1,” you can say “give me the value stored under the key name.”

That makes dictionaries different from lists.

Why Python dictionaries matter

Dictionaries matter because many kinds of data are naturally label-based rather than position-based.

For example, if you want to represent a product, this is much clearer:

product = {
“name”: “Laptop”,
“price”: 999.99,
“stock”: 12
}

than trying to remember something like this:

product = [“Laptop”, 999.99, 12]

The list version works, but you must remember:

  • index 0 is the name
  • index 1 is the price
  • index 2 is the stock

The dictionary version is easier to read and easier to maintain.

Dictionaries are especially useful when:

  • values have meaningful labels
  • order is less important than identification
  • you want quick access by name
  • the structure represents real-world objects
  • data comes from forms, settings, or APIs

For beginners, dictionaries are one of the most practical structures in Python.

How to create a dictionary in Python

A dictionary uses curly braces {} and stores pairs in the form:

key: value

Example:

student = {
“name”: “Emma”,
“age”: 21,
“grade”: “A”
}

This is a valid dictionary with three key-value pairs.

You can also create an empty dictionary:

data = {}

This is useful when you want to build the dictionary step by step later.

Keys and values explained simply

A dictionary consists of keys and values.

  • the key is the label
  • the value is the data stored under that label

Example:

car = {
“brand”: “Toyota”,
“year”: 2020,
“electric”: False
}

Here:

  • "brand""Toyota"
  • "year"2020
  • "electric"False

You use the key to reach the value.

This is one of the most important beginner ideas in dictionaries:

you do not access items by position, but by key

Accessing dictionary values

To access a value in a dictionary, use the key inside square brackets.

Example:

person = {
“name”: “Alice”,
“age”: 25,
“city”: “Budapest”
}print(person[“name”])
print(person[“age”])

Output:

Alice
25

This means:

  • person["name"] gives "Alice"
  • person["age"] gives 25

This is one of the main reasons dictionaries are so readable. The key explains what the value means.

How dictionaries differ from lists

A list is based on numeric indexes:

fruits = [“apple”, “banana”, “orange”]
print(fruits[0])

A dictionary is based on keys:

person = {“name”: “Alice”, “age”: 25}
print(person[“name”])

Key differences:

  • lists use positions
  • dictionaries use labels
  • lists are best for ordered sequences
  • dictionaries are best for structured labeled data

Both structures are useful, but they solve different kinds of problems.

Dictionary keys must be unique

In a dictionary, each key must be unique. You cannot have the same key twice with separate meanings.

Example:

data = {
“name”: “Alice”,
“name”: “Emma”
}print(data)

The second "name" replaces the first one.

The result will effectively be:

{‘name’: ‘Emma’}

This matters because dictionaries are designed for one value per key.

Adding new key-value pairs

You can add new entries to a dictionary by assigning a value to a new key.

Example:

person = {
“name”: “Alice”,
“age”: 25
}person[“city”] = “Budapest”

print(person)

Output:

{‘name’: ‘Alice’, ‘age’: 25, ‘city’: ‘Budapest’}

This works because "city" did not exist before, so Python creates it.

This is one of the most useful beginner dictionary operations.

Changing existing values

If the key already exists, assigning a new value updates it.

Example:

person = {
“name”: “Alice”,
“age”: 25
}person[“age”] = 26

print(person)

Output:

{‘name’: ‘Alice’, ‘age’: 26}

This means:

  • if the key is new, it gets added
  • if the key already exists, its value gets updated

That behavior is very important in practical dictionary use.

Removing items from a dictionary

There are several ways to remove data from a dictionary.

Using del

The del statement removes a key-value pair by key.

Example:

person = {
“name”: “Alice”,
“age”: 25,
“city”: “Budapest”
}del person[“city”]

print(person)

Output:

{‘name’: ‘Alice’, ‘age’: 25}

Using pop()

The pop() method removes a key and returns its value.

Example:

person = {
“name”: “Alice”,
“age”: 25,
“city”: “Budapest”
}removed_value = person.pop(“age”)

print(removed_value)
print(person)

Output:

25
{‘name’: ‘Alice’, ‘city’: ‘Budapest’}

This is useful when you want to remove something and still keep the removed value.

Using clear()

The clear() method removes everything from the dictionary.

Example:

person = {
“name”: “Alice”,
“age”: 25
}person.clear()
print(person)

Output:

{}

The len() function with dictionaries

The len() function returns the number of key-value pairs in a dictionary.

Example:

person = {
“name”: “Alice”,
“age”: 25,
“city”: “Budapest”
}print(len(person))

Output:

3

This counts pairs, not characters or something else.

It is useful for:

  • checking size
  • validating required fields
  • measuring collected data

Checking if a key exists

You can use in to check whether a key exists in a dictionary.

Example:

person = {
“name”: “Alice”,
“age”: 25
}print(“name” in person)
print(“city” in person)

Output:

True
False

This is very useful in if statements:

if “age” in person:
print(“Age is available”)

The opposite check uses not in:

if “city” not in person:
print(“City is missing”)

The get() method in Python dictionaries

A common beginner issue is trying to access a missing key.

For example:

person = {
“name”: “Alice”
}print(person[“age”])

This causes an error because "age" does not exist.

A safer alternative is get():

person = {
“name”: “Alice”
}print(person.get(“age”))

Output:

None

You can also give a default value:

person = {
“name”: “Alice”
}print(person.get(“age”, “Not available”))

Output:

Not available

This makes get() very useful when keys may or may not exist.

Looping through a dictionary

Dictionaries work very well with loops.

Looping through keys

By default, a for loop over a dictionary goes through the keys.

Example:

person = {
“name”: “Alice”,
“age”: 25,
“city”: “Budapest”
}for key in person:
print(key)

Output:

name
age
city

Looping through values

Use .values() to loop through the values.

Example:

person = {
“name”: “Alice”,
“age”: 25,
“city”: “Budapest”
}for value in person.values():
print(value)

Output:

Alice
25
Budapest

Looping through keys and values together

Use .items() to get both at once.

Example:

person = {
“name”: “Alice”,
“age”: 25,
“city”: “Budapest”
}for key, value in person.items():
print(key, value)

Output:

name Alice
age 25
city Budapest

This is one of the most useful dictionary patterns in Python.

The keys(), values(), and items() methods

These three methods are central to dictionary looping and inspection.

keys()

Returns the keys.

person = {“name”: “Alice”, “age”: 25}
print(person.keys())

values()

Returns the values.

person = {“name”: “Alice”, “age”: 25}
print(person.values())

items()

Returns key-value pairs.

person = {“name”: “Alice”, “age”: 25}
print(person.items())

Beginners do not need to memorize the internal object type returned here. The practical point is what each method gives access to.

Dictionary values can be different data types

Just like lists, dictionaries can store values of different types.

Example:

user = {
“name”: “Daniel”,
“age”: 31,
“is_admin”: False,
“score”: 98.5
}print(user)

This is one reason dictionaries are so practical. They can represent a small data record with multiple kinds of values.

Nested dictionaries

A dictionary can contain another dictionary as a value.

Example:

user = {
“name”: “Alice”,
“contact”: {
“email”: [email protected],
“phone”: “123456789”
}
}print(user[“contact”][“email”])

Output:

alice@example.com

This is called a nested dictionary.

Beginners do not need to master nested structures immediately, but it is useful to know that dictionaries can represent more complex real-world data.

Dictionaries with lists inside them

A dictionary value can also be a list.

Example:

student = {
“name”: “Emma”,
“grades”: [85, 90, 78]
}print(student[“grades”])
print(student[“grades”][0])

Output:

[85, 90, 78]
85

This is very common in real programs because structured objects often contain collections.

Real beginner examples with dictionaries

Example 1: Simple profile

profile = {
“name”: “Liam”,
“age”: 22,
“city”: “London”
}print(profile[“name”])
print(profile[“city”])

Example 2: Add a new field

product = {
“name”: “Mouse”,
“price”: 19.99
}product[“stock”] = 50
print(product)

Example 3: Update a value

settings = {
“theme”: “light”,
“language”: “English”
}settings[“theme”] = “dark”
print(settings)

Example 4: Loop through a dictionary

car = {
“brand”: “Toyota”,
“year”: 2020,
“electric”: False
}for key, value in car.items():
print(key, “:”, value)

Example 5: Check for a key

person = {
“name”: “Alice”,
“age”: 25
}if “age” in person:
print(“Age exists”)

These examples cover the most important beginner dictionary patterns.

Common beginner mistakes with Python dictionaries

Several mistakes happen often when beginners start using dictionaries.

Using a missing key directly

Incorrect:

person = {“name”: “Alice”}
print(person[“age”])

This causes an error because "age" is missing.

Safer version:

print(person.get(“age”))

Confusing keys and values

Example:

person = {“name”: “Alice”}
print(“Alice” in person)

This returns False because in checks keys by default, not values.

"name" in person is true, but "Alice" in person is not.

Forgetting quotation marks around string keys

Incorrect:

person = {
name: “Alice”
}

This causes an error unless name is a variable.

Correct:

person = {
“name”: “Alice”
}

Expecting dictionary order to behave like list indexing

A dictionary is not accessed with numeric positions the way a list is.

Incorrect thinking:

person = {“name”: “Alice”, “age”: 25}
print(person[0])

This does not work because dictionaries use keys, not indexes.

Reusing the same key twice

Example:

data = {
“name”: “Alice”,
“name”: “Emma”
}

Only one of those values survives. Keys must be unique.

Dictionary methods beginners should know first

At the beginner stage, the most useful dictionary tools are:

  • get()
  • keys()
  • values()
  • items()
  • pop()
  • clear()

You do not need to learn every dictionary method immediately. These are enough for a strong foundation.

Dictionaries versus lists

This is one of the most important beginner comparisons.

Use a list when:

  • order matters
  • you want to access items by position
  • the values are a sequence of similar items

Example:

fruits = [“apple”, “banana”, “orange”]

Use a dictionary when:

  • values have meaningful labels
  • you want to access data by name
  • the structure represents a record or object

Example:

person = {
“name”: “Alice”,
“age”: 25
}

In real code, lists and dictionaries are often used together.

Why dictionaries are useful in real programs

Dictionaries are one of the most practical structures in Python because real data is often label-based.

Common examples include:

  • user profiles
  • settings
  • product details
  • configuration data
  • API responses
  • student records
  • contact information
  • menu definitions
  • counters by category

When you need meaningful names for values, dictionaries are often the right tool.

Good beginner habits with dictionaries

A few habits make dictionary code cleaner and easier to maintain.

Use clear key names

Good:

user = {“first_name”: “Emma”, “age”: 21}

Less clear:

user = {“a”: “Emma”, “b”: 21}

Keep the structure consistent

If one user dictionary uses "name" and another uses "full_name" for the same idea, the code becomes harder to manage.

Use get() when keys may be missing

This avoids unnecessary errors and makes your code more robust.

Loop with items() when you need both key and value

This is cleaner than looping over keys and then looking up each value manually.

Print the dictionary while learning

When unsure what changed, print the whole dictionary and inspect it.

What you should understand before moving on

Before continuing to the next lesson, you should be comfortable with the following:

  • a dictionary stores key-value pairs
  • dictionaries use curly braces
  • values are accessed by key, not by index
  • keys should be unique
  • you can add and update values with assignment
  • del, pop(), and clear() remove data
  • len() returns the number of pairs
  • in checks whether a key exists
  • get() safely reads optional keys
  • items() is useful for looping through keys and values together

If these ideas are clear, the next lesson on functions will be much easier, because functions often receive and return structured data, including lists and dictionaries.

A Python dictionary is a collection of key-value pairs that allows you to store structured data with meaningful labels. For beginners, dictionaries are one of the most useful data structures because they make code more readable and more natural for real-world information such as names, ages, settings, product details, and user profiles.

Dictionaries are powerful because they let you access values by name instead of by position, and they work well with loops, conditions, and nested data structures. Once you understand how to create, update, access, and loop through dictionaries, you have a much stronger foundation for practical Python programming.


Image(s) used in this article are either AI-generated or sourced from royalty-free platforms like Pixabay or Pexels.

This article may contain affiliate links. If you purchase through these links, we may earn a commission at no extra cost to you.

Weekly briefing

Get the weekly RF & IT briefing

Radio guides, RF calculators, AI, Windows, Linux and satellite communication explainers. One useful email per week. No spam.

Similar Posts