Hit enter after type your search item

Python dictionary: Explained with 11 examples

What is Python dictionary?

The dictionary is a data type in Python that is used to store data in key/value pairs.

If you have worked in other languages like PHP, you have worked with associative arrays ; the dictionary is like that.

A dictionary can be created as follows:

  • In above example, the telephone_dir is the dictionary name.
  • The names Mike, and Haynes are keys and numbers 1234 and 4567 are their respective values.
  • Each key/value pair is separated by a comma.
  • While key and value is separated by a colon (:).
  • The keys in dictionary act as an index that are unlike sequences, for example, list or tuples where only numbers act as an index.
  • The keys in dictionaries can be numbers or strings. Using the string as index makes it quite useful in establishing the relationship with their respective values.
  • The keys in a dictionary are unique.

See the following examples of creating, accessing, deleting and using other methods related to dictionaries in Python.

An example of creating a dictionary

In this example, a dictionary is created with three elements. The dictionary name is tel_dir where name will act as keys and telephone numbers as values:

Python dictionary

See online demo and code

The code for creating and displaying a dictionary:


 

Accessing a specific element of dictionary example

By giving the key of an element in square brackets, you may access the specific element of a dictionary. See this example:

dictionary specific element

See online demo and code

The following code is used to access:


 

Using a for loop to iterate through dictionary elements

In this example, a for loop is used for accessing and displaying the dictionary elements. The items() method is used to retrieve the key and related value at the same time, as shown in the example below:

dictionary for loop

See online demo and code

The code:


 

You see, first, I created the same dictionary as in above example. After that, a for loop is used where items() method is used with dictionary name. The key is assigned to name while the value to numbers, that are displayed by using the print function.

Getting total number of dictionary items example

You may use the len method to get the total number of elements in the specified dictionary. The following example demonstrates how you can use it:

dictionary length

See online demo and code

This is how the length is taken by using len() method:


 

Getting keys of a dictionary by using keys method

By using the dictionary keys() method, you may get the keys of the specified dictionary. See an example below for accessing the same dictionary keys as used in above example:

dictionary keys

See online demo and code

The code:

print (list(tel_dir.keys()))

As this reruns “list” of the keys in given dictionary, the keys are enclosed in square brackets just like an ordinary list does.

Accessing only values example

Similarly, you may access only the values of given dictionary by using the values()  method. See a demonstration below:

dictionary value

See online demo and code

The code:


 

Just like keys method, the values return the list of values, so enclosed in square brackets in the output.

An example of removing all dictionary elements

By using the clear() method, you may remove all elements of the specified dictionary. Just write the name of dictionary you want to remove elements for, as shown in the example below:

Python dictionary clear

See online demo and code

The code:


 

You can see, the second print function displayed an empty dictionary after executing the clear() Python method.

Deleting a specific dictionary item example

Not only you can remove all elements by using the clear method, but you may also remove specific elements by using the key in the del() method.

See the example below with the same dictionary as I have been using in above examples:

dictionary del

See online demo and code

The code for deleting a specific element:


 

You can see in the second row of output, the record of the Haynes does not exist.

An example of sorting a Python dictionary

You may use the sorted() method to sort the dictionary items. In this example, the same dictionary is used and its keys are displayed before and after using the sorted method:

dictionary sorted

See online demo and code

The code for sorting:


 

Creating dictionaries by dict() constructor

The dictionaries in Python can also be created by using the dict() constructor.  It will build a dictionary from the sequences of key values pairs.

One of the benefits of using this is you may create dictionaries by using dict comprehension. I will show an example after this one, first have a look at simply creating a dictionary by dict() constructor:

dict

See online demo and code

The code:


 

An example of dict comprehension

In this example, dict comprehension is used for creating a dictionary.

dict comprehension

See online demo and code

The code:


 

You can see, the keys are the items in given sequence while values are the cube of each respective item. Similarly, you may set a formula there for creating a dictionary.

This div height required for enabling the sticky sidebar