DEV Community

Cover image for Python Dictionary
Code Leaks
Code Leaks

Posted on

Python Dictionary

Python dictionary
Python has many types of data structures. Python Dictionary is one of the implementations of Python's data structures. It is also known as an associative array, and it is an unordered collection of data values. Unordered data collections also include numbers, string, tuples.
Some of these unordered data collections methods that can be used in Python. Checking if a string contains substring, strinp() method to remove whitespaces, round numbers, and count() function for list and string, etc.
It holds key-value pairs. Every pair inside a dictionary maps the key to its associated value. This process makes it more optimized. Keys can be a single element, but values can be multiple elements like a list, number, or other value.
Key Points:
• Identical keys are not allowed in the dictionary. For knowing if key already exist in dictionary or not. Check if key exists already.
• Keys to be entered in the dictionary must be immutable, like numbers, strings, or tuples. At the same time, values can be of any type.
• In a dictionary, keys are treated as sensitive.
• Keys in the dictionary do not allow polymorphism.
Dictionary Mapping as a switch case statement to control the program flow.
There are many operations performed on dictionaries. Let's have a look at them too.
Creating a Dictionary
Dictionary can be created as a group of key-value pairs inside curly brackets {pairs}. Each key-value pair will be separated by a comma like {key1: value1, key2: value2}, and each key and value will be separated by a colon between them like {key: value}.
For example:
Alt CDS
There are many types of dictionary exists. We can create a regular dictionary, empty dictionary, dictionary with identical keys, dictionary with mixed keys, dictionary with dict() function, dictionary in pairs, nested dictionary, etc. Let's create all types of dictionaries in the code example.
Code Example

Output

If a user wants to add items in the dictionary manually, he can do this too. We can add items in the dictionary one by one and can update the existing items as well. Let's have a look at a coding example.

Code Example

Output

Accessing items of a Dictionary
After creating a dictionary, we can access it or its items by using different ways using keys as an index, get() method, etc. So, let's look at an example.
Code Example

Output

Remove/Delete items from a Dictionary.
We saw how we can add items in a dictionary now; we will see how we can remove/delete items from a dictionary. We can remove/delete items from a dictionary in different ways like using the del keyword, using pop() method, using popitem() method, using clear() method, etc. So, let's look at an example.
Code Example

Output

Conclusion
In conclusion, we had a discussion over Python Dictionary in this tutorial. We saw how we could create a dictionary and perform different operations on it.
Like we performed get() function operation to access items of the dictionary, del keyword, pop() method, popitem() to delete/remove the item from a dictionary, clear() method to delete entire dictionary, and so on.
Python dictionaries have many more unlimited methods; using them, a programmer can perform different operations on a dictionary according to his requirements.
How to use Dictionary in JavaScript?

Top comments (0)