When we start learning Python, lists are often one of the first data structures we use:
student = ["Laura", 24, "Medellín"]
Enter fullscreen mode Exit fullscreen mode
This works, but what does each position represent?
We need to remember that:
- Position
0contains the name. - Position
1contains the age. - Position
2contains the city.
A dictionary provides a clearer alternative because every value is identified by a descriptive key.
Creating a dictionary
A dictionary stores information as key-value pairs:
student = {
"name": "Laura",
"age": 24,
"city": "Medellín"
}
Enter fullscreen mode Exit fullscreen mode
Instead of asking for the value at position 0, we can ask directly for the value associated with "name":
print(student["name"])
Enter fullscreen mode Exit fullscreen mode
Output:
Laura
Enter fullscreen mode Exit fullscreen mode
A useful way to remember the difference is:
A list position tells you where a value is. A dictionary key tells you what the value means.
Updating and adding information
Python dictionaries are mutable, so their content can change after creation.
To update an existing value:
product = {
"name": "Keyboard",
"price": 120000
}
product["price"] = 110000
Enter fullscreen mode Exit fullscreen mode
To add a new key-value pair, use the same syntax with a key that does not exist yet:
product["available"] = True
Enter fullscreen mode Exit fullscreen mode
The resulting dictionary is:
{
"name": "Keyboard",
"price": 110000,
"available": True
}
Enter fullscreen mode Exit fullscreen mode
Safely reading values with get()
Using square brackets requires the key to exist:
print(student["email"])
Enter fullscreen mode Exit fullscreen mode
If "email" is missing, Python raises a KeyError.
The get() method provides a safer alternative:
print(student.get("email"))
Enter fullscreen mode Exit fullscreen mode
It returns None when the key is missing.
You can also define a default value:
print(student.get("email", "Not registered"))
Enter fullscreen mode Exit fullscreen mode
Output:
Not registered
Enter fullscreen mode Exit fullscreen mode
To check whether a key exists, use the in operator:
if "city" in student:
print(student["city"])
Enter fullscreen mode Exit fullscreen mode
Keys, values, and pairs
Python provides three useful dictionary methods:
course = {
"name": "Python Basics",
"duration": "4 weeks",
"format": "Online"
}
print(course.keys())
print(course.values())
print(course.items())
Enter fullscreen mode Exit fullscreen mode
They provide different views of the same information:
-
keys()returns the keys. -
values()returns the stored values. -
items()returns complete key-value pairs.
Looping through a dictionary
The items() method is especially useful when working with a for loop:
capitals = {
"Colombia": "Bogotá",
"Peru": "Lima",
"Argentina": "Buenos Aires"
}
for country, capital in capitals.items():
print(f"{country}: {capital}")
Enter fullscreen mode Exit fullscreen mode
Output:
Colombia: Bogotá
Peru: Lima
Argentina: Buenos Aires
Enter fullscreen mode Exit fullscreen mode
During every iteration, Python assigns the current key to country and its associated value to capital.
What is a set?
A set is a collection that stores unique values.
Consider this list:
languages = ["Python", "Rust", "Python", "JavaScript", "Rust"]
Enter fullscreen mode Exit fullscreen mode
We can remove repeated values by converting it into a set:
unique_languages = set(languages)
print(unique_languages)
Enter fullscreen mode Exit fullscreen mode
The set keeps only one occurrence of each language.
Sets are useful when you need to:
- Remove duplicate values.
- Check whether a value is present.
- Compare groups of elements.
- Work with unique categories or identifiers.
Unlike lists, sets do not support indexes or slicing.
An important detail
Empty braces create an empty dictionary:
empty_dictionary = {}
Enter fullscreen mode Exit fullscreen mode
To create an empty set, use set():
empty_set = set()
Enter fullscreen mode Exit fullscreen mode
This is a common source of confusion for Python beginners.
Which structure should you choose?
Use a list when position and order are important.
Use a dictionary when every value should have a descriptive name.
Use a set when you need unique values and repetitions do not matter.
Understanding this distinction makes programs easier to read, maintain, and extend.
The original Spanish guide includes a step-by-step explanation with additional examples:
What was more confusing when you first learned Python: dictionaries or sets?
답글 남기기