Python Dictionaries

A Dictionary in Python contains a value with its key.

Dictionary is different from the other type of sequences in Python like Lists, Tuples, etc.

From the creation to accessing values, a Dictionary is somewhat different than other sequences but it does share some of the basic “sequential” properties with them too.

There are some advantages associated with Dictionaries over the other type of sequences: –

  • Dictionaries have this advantage of having a key associated with the values because when accessing values, you don’t have to know its index number.
  • Also, you can check whether a key or its value exists or not.

A Dictionary is a mutable sequence which means that it can be changed or updated. It is unordered but indexed, i.e., items can be accessed but are not in order.

Creating Dictionaries

The value and its key in a Dictionary are separated by a colon (:). All of the key – value pairs are separated by a comma and everything is enclosed in curly braces.

Example

Python creating dictionary

A set of values can be associated to a single key like this: –

Example

Python- creating dictionary single key

Moreover, the constructor dict() can also be used to create a dictionary like this: –

Example

Python - creating dictionary dict function

Let’s see how you can access values in a Dictionary.

Accessing Values

Values in a Dictionary are accessed by their Keys.

Example

Python- accessing values of dictionary

Values with no keys cannot be accessed. If you try to access a value with a key that does not exist in the dictionary, you will get an error.

Similarly, values can be accessed by a method called get() by passing valid keys as an argument.

Example

Python - accessing values using get function

An example in the “Creating Dictionaries” section showed how you can associate multiple values to a single key. Let’s see how you can access the values: –

Example

Python - accessing multiple values of dictionary

Updating Dictionaries

As already discussed, items from a Dictionary can be changed or removed or more items can be added in a Dictionary.

Adding Items

Values can be added easily to a Dictionary. A single value can be added with the help of its key.

Example

Python - adding values in empty dictionary

You can also add multiple values to a single key like this –

Example

Python - adding multiple values single key

Removing Items

There are many ways to remove items from a Dictionary. The method pop() removes a specific key with its value. However, the popitem() method removes the last inserted key-value pair from the dictionary. However, in the versions older than Python 3.7. It was used to remove a random key-value pair and return it.

The function del was used to remove the whole dictionary and it can also be used to remove a particular key-value pair.

Example

Python - remove functions using del

Changing Item Values

Item values can be changed by using the key of that particular value.

Example

Python - updating item values dictionary

Nested Dictionary

A dictionary can contain multiple dictionaries inside it. This type of dictionary is called a Nested Dictionary. You can create a Nested dictionary like this: –

Example

Python - nested dictionary

You can also add multiple separately created dictionaries into a single nested Dictionary.

Example

Python - combining separate multiple dictionaries

Some Useful Methods/Functions

Let us see, how you can use multiple useful functions to get most functionality out of Dictionaries.

Check the length of a Dictionary

The method len() can be used to get the length of a Dictionary.

Example

Python - len() function for dictionary

Empty a Dictionary

The method clear() can be used to delete all the items from a Dictionary.

Example

Python - image empty dictionary clear()

Copy a Dictionary

Dictionaries can be copied by using the method copy(). This can be done like this: –

Example

Python - copy dictionary

Using a “for” Loop

A for loop can be used to get the keys and values from the dictionary. Looping simply through a dictionary will only return keys but values can also be obtained with help of values() method.

Example

Python - for loop only keys dictionary

The above code will only print keys from the dictionary. To get the values from the dictionary, this method can be used: –

Example

Python - for loop only values dictionary

Both keys and values as key-value pairs can also be extracted using items() method.

Example

Python for loop both keys values dictionary

Dictionary Methods

We have already talked about some of the functions/methods, here is a table of all of the functions that can be used for Dictionaries: –

Python Dictionaries method
Method Description
get() This will return the value of a particular key that is passed as an argument in the function.
keys() This returns all the keys from the dictionary.
values() This returns all the values from the dictionary.
pop() This removes the value associated with key that is passed as an argument in the function.
popitem() This removes the last element from the dictionary. However, this is not true for the versions of Python older than Python 3.7. In the older versions, it removes and returns an arbitrary key and value pair.
update() This updates the dictionary with a particular key and value pair.
setdefualt() This will return the value of the key. If there is no key present, it will update the dictionary with a pre-specified key and value pair.
items() This will return a list that will contain all the key and value pairs present in the dictionary.
copy() This returns a copied dictionary.
fromkeys() This will return a dictionary with particular keys and their values.
clear() This function deletes all the elements from a dictionary.

Tutorials for all brains!