Saltar al contenido principal
InicioTutorialesPython

Python Dictionaries Tutorial

Learn how to create a dictionary in Python.
25 jun 2020  · 3 min leer

Dictionary is a built-in Python Data Structure that is mutable. It is similar in spirit to List, Set, and Tuples. However, it is not indexed by a sequence of numbers but indexed based on keys and can be understood as associative arrays. On an abstract level, it consists of a key with an associated value. In Python, the Dictionary represents the implementation of a hash-table.

What are Keys?

As shown in the figure below, keys are immutable ( which cannot be changed ) data types that can be either strings or numbers. However, a key can not be a mutable data type, for example, a list. Keys are unique within a Dictionary and can not be duplicated inside a Dictionary. If it is used more than once, subsequent entries will overwrite the previous value.

keys

Key connects with the value, hence, creating a map-like structure. For example, remove keys from the picture; all you are left with is a data structure containing a sequence of numbers. Dictionaries, therefore, hold a key: value pair at each position.

A Dictionary is represented by a pair of curly braces {} in which enclosed are the key: value pairs separated by a comma.

Creating a Dictionary

Syntax

As a refresher, here is a recipe for creating a Dictionary:

my_dict = {
  "key1":"value1",
  "key2":"value2"
}

In the above syntax, both the keys and the values are strings.

Example of Creating a Dictionary

pop = [30.55, 2.77, 39.21]

countries = ["afghanistan", "albania", "algeria"]

Let's convert the population data to a Dictionary. To create the Dictionary, you will use the curly brackets. Next, inside the brackets, you will have a bunch of key:value pairs. In this case, the keys are country names, and values are the corresponding populations.

The first key is Afghanistan, and its corresponding value is 30.55. Notice the colon that separates the key and value pair. Let's do the same for the other two key:value pairs and store the result in the variable world.

Now, to find the population for Albania, simply type world, and then the string Albania inside square brackets. In other words, you pass the key in square brackets and get the corresponding values.

world = {"afghanistan":30.55, "albania":2.77, "algeria":39.21}

world["albania"]
2.77

This approach is not only intuitive, but it is also very efficient because Python can look up these keys very fast, even for huge dictionaries.

Interactive Example of Creating a Dictionary

In the below example, you will use the strings in countries and capitals, create a Dictionary called europe with 4 key:value pairs. Here the countries will be the keys, and capitals will be the values. Beware of capitalization! Make sure you use lowercase characters everywhere. Finally, print out europe to see if the result is what you expected.

# Definition of countries and capital
countries = ['spain', 'france', 'germany', 'norway']
capitals = ['madrid', 'paris', 'berlin', 'oslo']

# From string in countries and capitals, create dictionary europe
europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo'}

# Print europe
print(europe)

When you run the above code, it produces the following result:

{'norway': 'oslo', 'germany': 'berlin', 'france': 'paris', 'spain': 'madrid'}

Try it for yourself.

To learn more about dictionaries in Python, please see this video from our course Intermediate Python.

This content is taken from DataCamp’s Intermediate Python course by Hugo Bowne-Anderson.

Temas

Python Courses

Course

Introduction to Python

4 hr
5.7M
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course
Ver másRight Arrow
Relacionado

tutorial

Python Dictionaries Tutorial: The Definitive Guide

Learn all about the Python Dictionary and its potential. You will also learn how to create word frequency using the Dictionary.
Aditya Sharma's photo

Aditya Sharma

6 min

tutorial

Python Dictionary Tutorial

In this Python tutorial, you'll learn how to create a dictionary, load data in it, filter, get and sort the values, and perform other dictionary operations.
DataCamp Team's photo

DataCamp Team

14 min

tutorial

Tuples in Python Tutorial

Learn to use and unpack Tuples in Python.
DataCamp Team's photo

DataCamp Team

3 min

tutorial

Python Tuples Tutorial

Learn about Python tuples: what they are, how to create them, when to use them, what operations you perform on them and various functions you should know.
Sejal Jaiswal's photo

Sejal Jaiswal

10 min

tutorial

Python Datetime Tutorial

Learn how to create a datetime object.
DataCamp Team's photo

DataCamp Team

2 min

tutorial

Python While Loops Tutorial

Learn how while loop works in Python.
DataCamp Team's photo

DataCamp Team

4 min

See MoreSee More