[Python Basic] Dictionaries — Part 1

We practice how to work with dictionaries in Python. Understanding how to access information once it is inside a dictionary and how to modify that information allows us to make a better data structure.

A Ydobon
3 min readDec 18, 2019
CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=462068

A Simple Dictionary Example

Dictionaries are Python’s implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value.

We can define a dictionary by enclosing a comma-separated list of key-value pairs in curly braces ({}). A colon (:) separates each key from its associated value:

country_code = {
'Nigeria': 234,
'Kazakhstan': 7,
'France': 33,
'Brazil': 55,
'United States': 1
}
print(country_code['France'])

The dictionary country_code contains the names of countries and the associated international phone-call codes. The print statement produces the country code of France, which is 33. Here the country names are keys and the corresponding international codes are values.

Working with Dictionaries

The first thing to remember for a dictionary is that it is a collection of key-value pairs. The key should be unique, and the value can be any object that we can create in Python, that is it can be a number, a string, a list or even another dictionary.

The second thing to remember for a dictionary is that it is wrapped by curly braces ({}), each key is connected to its value by a colon (:) and individual key-value pairs are separated by commas.

Accessing Values in a Dictionary

To get the value associated with a key, we need the name of the dictionary and the place the key inside a set of square brackets ([]). We use a different version of defining a dictionary. Note the differences.

country_code = dict([
('Nigeria', 234),
('Kazakhstan', 7),
('France', 33),
('Brazil', 55),
('United States', 1)
])
print(country_code['Brazil'])

Note that unlike lists, dictionaries do not support indexing or slicing because there is no order in dictionaries.

Adding New Key-Value Pairs

Adding an entry to an existing dictionary is simply a matter of assigning a new key and value:

country_code['Japan'] = 81print(country_code['Japan'])

Another way to adding new pairs is to use the built-in dictionary method update() .

country_code = dict([
('Nigeria', 234),
('Kazakhstan', 7),
('France', 33),
('Brazil', 55),
('United States', 1),
('Japan', 82)
])
new_code = {'South Korea':82, 'Japan':81}
country_code.update(new_code)
print(country_code)

The main purpose of the update() method is not to add new pairs, but to update the dictionary. Therefore, the key in new_code is not in the dictionary (‘South Korea’ in this example), it will be added to the dictionary, while the key in new_code is already in the dictionary, this information is updated (‘Japan’ in this example). BTW, the actual code for Japan is 81, not 82.

Modifying Values in a Dictionary

Since we showed how to update the dictionary, we can use the update() method to modify a value in the dictionary. Or we may use the direct way to update a value.

country_code = dict([
('Nigeria', 234),
('Kazakhstan', 7),
('France', 33),
('Brazil', 55),
('United States', 1),
('Japan', 82)
])
country_code['Japan'] = 81print(country_code)

Starting with an Empty Dictionary

It is not always true that we start from an empty dictionary, but sometimes we should. Then we add each key-value pairs on its line.

country_code = {}country_code['Nigeria'] = 234
country_code['Kazakhstan'] = 7
print(country_code)

Removing Key-Value Pairs

When we no longer need a piece of information stored in a dictionary, we can use the del statement to delete a key-value pair.

country_code = dict([
('Nigeria', 234),
('Kazakhstan', 7),
('France', 33),
('Brazil', 55),
('United States', 1),
('Japan', 82)
])
del country_code['Japan']print(country_code)

--

--

A Ydobon
A Ydobon

No responses yet