Sep 25, 2022 By Team YoungWonks *
What is a Python Dictionary?
The Python Dictionary is a data structure that stores data as key-value pairs. Dictionaries, as opposed to Python lists, are a collection of unique keys associated with values. It doesn't keep duplicate keys. A Python dictionary's values are mapped using keys. The key-value pairs contain data of the specified data type, such as integers, strings, and so on. This data structure is also found in other programming languages, such as Java, and is known technically as a hash map.
In this Python tutorial, we will learn how to manipulate key-value pairs using Python Dictionary methods.
Python Dictionary Methods
There are several methods for manipulating or extracting data from a Python dictionary. We'll also learn a few built-in methods for changing the dictionary. Let us now learn these methods.
1. Create a Python Dictionary
Curly braces ({}) are used to create an empty dictionary, which contains key-value pairs separated by commas (,). A colon separates the key and values.
Syntax: dict = {}
2. Add key-value pairs into a dictionary
Using the syntax shown below, we can add new key-value pairs to a dictionary. The same value can be assigned to multiple keys.
Syntax: dict[new key] = value
Alternate Methods:
Syntax: dict.update( {new key:value}
We can use the built-in function described above to add new dictionary key-value pairs to a dictionary.
3. Delete key-values pairs into a dictionary
Key-value pairs can be removed from a dictionary.
a. Method 1: Removing the key automatically removes the values from a dictionary.
Syntax: del (dict[key])
b. Method 2: This method removes given key-value pairs.
Syntax: dict.pop(key)
c. Method 3: This method removes the last item from a dictionary.
Syntax: dict.popitem()
4. Update Dictionary values
Since dictionary is unique to keys we can only update the value for the keys.
Syntax: dict[key] = new value
5. Get Values of specified keys
We can use the get method to retrieve the corresponding value of the specified key. Here, we pass the key as a parameter to the function, which return value.
Syntax: dict.get(dictionary key)
6. Duplicate a dictionary
To create a copy of the dictionary, we can use the method given below.
Syntax: dict1 = dict.copy()
7. Get a list of keys and list of values separately
We can segregate the keys and values in a list format.
Syntax: dict1 = dict.keys()
dict2 = dict.values()
8. Get key-value pairs in a list format
This built-in method is used to convert a dictionary into a list of key-value tuple pairs. This method returns a dict items object that cannot be subscripted; iterating over it will result in a keyerror.
In order to get each tuple containing the key-value pairs, we have to use the built-in-list method to convert it into a list.
Syntax: dict1 = dict.items()
9. Iterate on a dictionary
We can run a for loop on a dictionary to obtain each key and value one by one.
Syntax: for i in dict:
print (i, dict[i])
Create a dictionary using a tuple.
Syntax: keys = (key1, key2, key3)
values = (value1, value2, value3)
dict.fromkeys(keys, values)
This method returns a dictionary having specified keys and values.
10. Empty a dictionary
We use the clear method to empty a dictionary.
Syntax: dict.clear()
11. Get values of specified key using the setdefault method
In this method, if the item is present in the dictionary it returns its value. Else, it returns a dictionary with new key and default value.
Syntax: dict.setdefault(key, value)
Mastering Python Dictionary Methods
In this blog, we unveiled the power and flexibility of dictionaries in Python programming. Dictionaries are essential for managing and organizing data in Python, making knowledge of their methods crucial for anyone looking to excel in coding. By enrolling in our Coding Classes for Kids, students can get a hands-on learning experience that emphasizes the importance of data structures, including dictionaries. Our Python Coding Classes for Kids specifically focus on mastering these techniques, providing a solid foundation for more advanced programming endeavors. Furthermore, for students eager to apply their coding skills in creative and interactive projects, our Raspberry Pi, Arduino and Game Development Coding Classes offer an engaging platform to explore the practical applications of Python, including the use of dictionaries in game development and physical computing projects.
*Contributors: Written by Aayushi Jayaswal; Lead image by Shivendra Singh