course content

Course Content

Introduction to Python

What Is a Dictionary?What Is a Dictionary?

In previous chapters, we dove into data structures that rely on numeric indices. Now, we're going to explore a structure that uses a key — whether it's a string, tuple, number, and so on — to index its values. This structure is known as a dictionary. Within dictionaries, data is stored in key-value pairs. Here are some key points to remember about dictionary keys:

  • You can use any immutable type as a dictionary key.
  • Tuples can serve as keys, but only if they contain strings, numbers, or other tuples.
  • A single dictionary won't have duplicate keys.
  • In Python, dictionaries are enclosed within curly brackets {}.

To illustrate, let's say we want to capture data about countries. The dictionary could use country names as keys, with corresponding values (like area and population) saved as tuples.

Got it? So, how do you retrieve a particular item from a dictionary? As touched upon earlier, you reference an item in a dictionary using its key. If the key is a string (as in our example), remember to wrap it in quotation marks. Just like with list or tuple indices, you'll place the key inside square brackets.

Everything was clear?

Section 4. Chapter 11