MarDevs introduces you to the Dictionary in Python

In this article, MarsDevs will present you with the Python Dictionary, its syntax, examples, and different operations on Python Dictionary.

What is a Python Dictionary?

In the Python dictionary, data is stored in a key-value pair format. It is the key and value that defines a dictionary. Now, the python dictionary is a mutable data type i.e it can be changed/updated. The dictionary resembles a real-world data arrangement in which there exists a given value for the specified key. 

The keys are the immutable python objects like strings, tuple, integer, etc. 

On the other hand, value can be of any type such as a list, tuple, integer, etc. In the dictionary, keys are unique, whereas values may or may not be unique. 

Things to keep in mind while working with python dictionaries, 

  • The dictionary key must be an immutable element.
  • Also, the keys are case-sensitive.

Creating a Python Dictionary

Creating a python dictionary is very simple. We need to place the given key-value pair within curly brackets ( {} ), key and value separated by colon ( : ) and multiple keys and values separated by comma ( , ).

Below are the different ways of creating a python dictionary,

We can also create a dictionary using the built-in function dict().

Access values in Python Dictionary

For accessing values in the dictionary, we use square brackets with a key for which the value is expected to be retrieved. 

If we try to access the value using the referenced key which is not present in the dict then we will face the KeyError as shown below example,

Update Python Dictionary

The dictionary can be updated in many ways like

  • Adding a new key-value pair.
  • Modifying the existing pair.
  • Adding multiple values to a single key.
  • Adding nested keys.

Delete elements in Dictionary

In python, we have many methods to delete elements in a dictionary or even the entire dictionary.

Using the del keyword we can delete the values for a specific key in the dictionary.

Now, in order to clear the entire dictionary we can use .clear() method.

Comprehension in Python Dictionary

Python Dictionary comprehension is the concept of creating a new python dictionary using the iterables. 

The syntax for creating python dictionary comprehension,

{key: value for var in iterable}

Here is the example showing the use of for-loop in the dictionary comprehension,

Iterate Python Dictionary

We can do the iteration in python using a for loop.

Here, we are using a for loop to iterate the values. 

Now, using a for loop with items() method for iteration.

Here, we used a for loop with values() method for iteration.

Properties of Dictionary Keys

There are no restrictions on python dictionary values, they can be any python object. But the same is not true for python dictionary keys.
In dictionary keys, duplicacy is not allowed. If you encounter duplicate keys then the last assignment will won.

Python dictionary keys are immutable; they can only be numbers, strings, or tuples.

Now some built-in functions in the Python Dictionary

In the dictionary, we have some built-in functions that will make work simple and efficient.

(i) cmp(dict1, dict2)

The cmp() function is used to compare two dictionaries and returns True if the first dictionary is greater than the second. Otherwise, it returns False. cmp() method works in Python 2.x but it;s python 3,x

(ii) len (dict)

As the name suggests, it is used to find the length of the dictionary. 

(iii) str(dict)

This method will print the string representation of the python dictionary.

(iv) all(dict)

This method will return True if all the keys in the dictionary are truthy.

(v) any(dict)

This method will return True if any of the dictionary keys are truthy

(vi) sorted(iterable, key, reverse)

This method will return the sorted list of keys present in the dictionary. Key and reverse are optional. 

Let’s discuss some more dictionary methods

  • dict.clear() - This method is used for removing every single element from the dictionary. 
  • dict.copy() - This method will return a copy of a given dictionary.
  • dict.pop() - This method removes an element from the specified key.
  • dict.get() - This method is used to get the value of the specified key.
  • dict.items() - This method returns a list of dictionary pair tuples.

dict.keys()- It will return the list containing dictionary keys.