Python language basics 17: dictionaries

Introduction

In the previous post we discussed how to store a number of elements in list collections. We saw how to create a list, how to append a new element to it and how to access a certain element within the list.

In this post we’ll look at the basics of another type of collection called a dictionary.

What is a dictionary?

You’ll probably know what the English word dictionary means. If you open an English-German dictionary then you’ll be able to look up what a certain English word means in German. The English word you want to look up is called the “key” and the corresponding German translation is called the “value”. You can always find the value by its key. Normally a language dictionary only contains unique keys, i.e. the same English lookup word only appears once.

Dictionary collections in Python work in much the same way. They have keys and each key has a value. Each key is unique. An important difference is that the keys in language dictionaries are normally sorted in alphabetical order so that a human can quickly look up a word. Key-values in Python dictionaries are on the contrary not sorted in any way by default. However, that’s not an issue for the program itself because it will quickly find any given key.

Dictionaries are also called key-value collections. The elements within a dictionary are called key-value pairs. In Java they go by the name “map” as keys are mapped to values. In C# they are also called dictionaries.

Dictionaries in Python

We saw in the previous post how the square brackets are used to construct and access a list. Dictionaries are slightly different. We’ll need to use curly braces instead for the declaration step:

myFirstDictionary = {}

We can also insert one or more elements within the curly braces. We’ll separate keys from values by colons:

myFirstDictionary = {"S": "Small", "M": "Medium", "L": "Large", "XL": "X-Large"}

The keys are S, M, L and XL. You’ll understand what the corresponding values are. Note that there’s no guarantee for how the elements will be sorted. If I print myFirstDictionary…:

print(myFirstDictionary)

…the I get the following:

{‘M’: ‘Medium’, ‘S’: ‘Small’, ‘XL’: ‘X-Large’, ‘L’: ‘Large’}

…which is definitely not how we declared it.

However, elements within dictionaries are normally not accessed by a numeric enumerator like in the case of lists. Instead we retrieve a value by its key as follows:

myFirstDictionary["S"]

…which gives “Small”.

We can add a new key-value pair using the brackets:

myFirstDictionary["XS"] = "X-Small"

If we later on access an existing key in the same way…:

myFirstDictionary["XL"] = "Very Large"

…then the corresponding value will be updated. This ensures that a single key cannot appear more than once.

In the next post we’ll look at how to iterate through lists and dictionaries using loops.

Read all Python-related posts on this blog here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: