Python Dictionary


In Python, a dictionary is a built-in data structure that allows you to store and retrieve values using keys. It is also known as an associative array or a hash map in other programming languages.

Dictionaries are mutable, unordered, and can contain elements of different data types.

Dictionaries are mutable, meaning that they can be changed after they are created.

This makes them a good choice for storing data that needs to be updated frequently.

Example to create and use a dictionary in Python:


Some of the common operations that you can perform on Python dictionaries:

Creating a dictionary

The dict() constructor is used to create a dictionary.

Accessing values by key

You can access the value associated with a key by using the key. Iterating over keys and values: You can iterate over the keys and values in a dictionary using a for loop.

Adding new items

You can add new items to a dictionary by using the update() method. Removing items: You can remove items from a dictionary by using the pop() method.

Clearing a dictionary

You can clear a dictionary by using the clear() method.

Testing for membership

You can test whether a key is present in a dictionary using the in operator. Getting the length of a dictionary: You can get the length of a dictionary using the len() method.



Dictionaries are useful when you want to associate a value with a unique key and retrieve it efficiently using that key.

They are commonly used for tasks such as counting occurrences of elements, mapping values, and grouping data.


Some examples of how dictionaries can be used in Python:

Storing data: Dictionaries can be used to store data in a way that is both efficient and easy to access.

For example, you could use a dictionary to store the names and ages of your friends.

Making decisions: Dictionaries can be used to make decisions based on the values that they store. For example, you could use a dictionary to store the names of all of the cities in a state and then use that dictionary to determine which city is the capital.

Grouping data: Dictionaries can be used to group data together based on common characteristics.

For example, you could use a dictionary to store the names of all of the students in a class and then use that dictionary to group the students together by their grade level.

Dictionaries are a powerful tool that can be used to store, manipulate, and analyze data in Python.

Enroll Now

  • Python Programming
  • Machine Learning