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

The Dictionary in Python

Python provides a data type dictionary (or Python 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 of any type of objects like a number, string or a list in Python dictionary.
  • The values can be accessed by using key rather than the index like in simple arrays.

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.
  • Python 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 or printing elements of dictionary

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

Output will be:

10000

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.

Output will be:

Salary of employee Shella is 5000

Salary of employee Mike is 10000

Salary of employee John is 15000

Python Dictionary methods

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

Length of dictionary

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

Output will be:

Length of dictionary: 3

Python dict clear() method

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

Output will be:

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.

Example of using items() method

Output will be:

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

Dictionary keys() method

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

Output will be:

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

Dictionary values() method

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

Output will be:

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

Also see – Tuple in Python | The Set

Was this article helpful?

Related Articles

Leave A Comment?