Python Dictionary: 7 examples to learn dict data type in 45 minutes

The Dictionary in Python

Python provides a data type dictionary (or dict) which is just like the associative arrays in some other languages. The Dictionary data types work with key – value pairs.

  • A key can be any type of objects, for example, a number, string or even a list in Python dictionary.
  • The values can be accessed by using key rather than the index like in simple arrays.

An example of Dictionary

Example of dictionary length method

This really helps in establishing a strong link between the dictionary keys and values, as explained in employee/salary example below.

If you require storing the salaries of employees in an array, an int-indexed based array can’t be the best choice and may always be confusing.

Whereas, using the key – value pair in the dictionary makes this task quite simple, as shown in the working examples of this chapter.

Syntax of creating a dictionary

Following is the syntax of Python dict:

dictionary_name = {key:value, key:value, …..}

For example

tel_dic = {‘Mike’: 1233456,’John’: 567890}

As you can see in the above example:

  • Each key is separated from its value by using a ‘:’.
  • keys are separated by a comma.
  • The dictionaries are enclosed in the curly brackets, i.e. {}.

Example of creating a dictionary

In the following example, we will create a python dict of employees with employee name and salary key/values.

You may also initialize above dictionary like that:

i.e. comma separated.

Accessing and printing elements of dictionary

The Dictionary elements can be accessed by using the keys as shown in the example below:

Python dictionary demo

See this example online

The Output:


How to iterate through a dictionary

We can loop through the dict just like a list. See example below of using a for loop to display employee dictionary. Note that, the Python dictionaries do not keep the order of values as created.

Python dict

See this example online

The Output of the above code:

Salary of employee Shella is 5000

Salary of employee Mike is 10000

Salary of employee John is 15000

The Dictionary methods

Following are a few methods of dictionary Python data type with examples.

Length of dictionary

The len() method returns the total number of items in the dictionary. See example below:

dictionary length

See this example online

The Output:

Length of dictionary: 3

The dict clear() method

The dict clear() method removes all elements of a dictionary. See example below:

See this example online

The Output:

Dictionary before clear {‘Shella’: 5000, ‘John’: 15000, ‘Mike’: 10000}

Dictionary after clear {}

Dictionary items method

The dict items() method returns the list of dictionary’s tuple pairs. This can be used in iterating through loops as well.

dictionary items

Example of using items() method

See this example online

The code Output:

Item method outputs:  dict_items([(‘John’, 15000), (‘Mike’, 10000), (‘Shella’, 5000)])

Dictionary keys() method

The keys() method of Python dictionary returns the keys in a dictionary. See example below:

See this example online

The Output:

Keys in employee dictionary:  dict_keys([‘Shella’, ‘John’, ‘Mike’])

Dictionary values() method

The dict values() method returns the values in a dictionary associated with keys.

See this example online

The output of above code:

Values in employee dictionary:  dict_values([15000, 10000, 5000])


Useful reading: Tuple in Python | The Set