Data Structures in Python: Dictionaries

Capítulo 17

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

Python dictionaries are an invaluable and powerful data structure that every programmer should know. They are a fundamental part of Python and are used in a variety of applications. In this section of our e-book course, we will discuss Python dictionaries in detail.

A dictionary in Python is an unordered collection of items. While other composite data types only have a value as an element, a dictionary has a key:value pair. The "Python" key and value are separated by a colon, and the set of key:value pairs is wrapped in curly braces {}. For example:

my_dictionary = {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
}

A dictionary key can be of any immutable type, such as a string, a number, or a tuple. The values ​​of a dictionary can be of any type and can be modified, which means that a dictionary is a mutable data structure.

Dictionaries are optimized to retrieve values ​​when the key is known. This is possible due to the fact that dictionaries in Python are implemented as hash tables.

To access the value of a specific key, you can use the following syntax:

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

print(my_dictionary["key1"]) # Output: value1

If you try to access a key that doesn't exist in the dictionary, Python will throw an error. To avoid this, you can use the get() method, which returns None if the key doesn't exist:

print(my_dictionary.get("nonexistent_key")) # Output: None

To add a new key:value pair to the dictionary, you can use the following syntax:

my_dictionary["key4"] = "value4"
print(my_dictionary) # Output: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}

To remove a key:value pair from a dictionary, you can use the keyword del:

from my_dictionary["key1"]
print(my_dictionary) # Output: {'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}

In addition, Python dictionaries have a variety of useful methods. For example, the keys() method returns a new view of the keys dictionaries. Similarly, the values() method returns a new view of all the values ​​in the dictionary.

print(my_dictionary.keys()) # Output: dict_keys(['key2', 'key3', 'key4'])
print(my_dictionary.values()) # Output: dict_values(['value2', 'value3', 'value4'])

In conclusion, Python dictionaries are a powerful and versatile tool. They allow you to efficiently store and retrieve values ​​and are fundamental to many aspects of Python programming. Whether you're a beginner or an experienced programmer, understanding dictionaries is essential to writing effective and efficient Python code.

In the next chapter of this e-book course, we'll explore data structures in Python even further, focusing on lists and tuples. Stay tuned!

Now answer the exercise about the content:

What is a dictionary in Python and how is it used?

You are right! Congratulations, now go to the next page

You missed! Try again.

A dictionary in Python is defined as an unordered collection of items, where each item is a key:value pair, used to efficiently store and retrieve values. Python dictionaries are implemented using hash tables, which optimizes lookup times when the key is known.

Next chapter

Data Structures in Python: Sets

Arrow Right Icon
Free Ebook cover System creation course with Python and Django complete
10%

System creation course with Python and Django complete

New course

176 pages

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.