A dictionary in Python is a collection of data that is unordered, indexed, and can be changed. A dictionary contains pairs of keys and values so it can have more than one element and holds different data types. Its syntax is key:value
.
- Creating a Dictionary
- Accessing elements of the dictionary
- Changing a value in a dictionary
- Get all keys in a dictionary
- Get all values in a dictionary
- Check if a certain key exists in a dictionary
- Determine dictionary length
- Adding an element to the dictionary
- Removing an element from a dictionary
- Copy a dictionary
- Nested Dictionaries
Creating a Dictionary
There are two ways to create a dictionary. The first one is declaring a variable and assigning to it the desired keys with the desired values and put them into curly brackets{}. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } print(Fruit)
Output:
{‘kind’: ‘Banana’, ‘color’: ‘Yellow’, ‘weight’: 0.5}
The second way is using the dict() constructor. Where the desired values are assigned to their keys using equals instead of colons and they are then passed to the dict() constructor. Let’s see the syntax:
Code:
Fruit = dict(kind = "Banana", color="Yellow", weight = 0.5 ) print(Fruit)
Output:
{‘kind’: ‘Banana’, ‘color’: ‘Yellow’, ‘weight’: 0.5}
Accessing elements of the dictionary
There are several ways to access variables inside a dictionary. The first way is to assign the name of the dictionary followed by square brackets[], where these brackets contain the key to the desired variable. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } z = Fruit["kind"] print(z)
Output:
Banana
The second way to access a dictionary value by using get() function. By assigning the dictionary name attached to it the get() function to a variable, where the key to the desired value is passed as a get() function parameter. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } z = Fruit.get("kind") print(z)
Output:
Banana
Changing a value in a dictionary
You can change a certain value in a dictionary by referring to its key. By writing the dictionary name followed by square brackets[] and assigning to it the new value, where the square brackets contain the key to the desired value. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } Fruit["weight"] = 0.2 print(Fruit)
Output:
{‘kind’: ‘Banana’, ‘color’: ‘Yellow’, ‘weight’: 0.2}
Get all keys in a dictionary
To get all the keys available in a dictionary you can use the keys() function. When the dictionary name is written attached to it the keys() function, it returns a display of all the keys as a list. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } print(Fruit.keys())
Output:
dict_keys([‘kind’, ‘color’, ‘weight’])
Another way to get the keys present in a dictionary by looping through the dictionary using for a loop. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } for x in Fruit: print(x)
Output:
kind
color
weight
Get all values in a dictionary
To get all the values in a dictionary you can use the values() function attached to the dictionary name. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } print(Fruit.values())
Output:
dict_values([‘Banana’, ‘Yellow’, 0.5])
Check if a certain key exists in a dictionary
To check whether the desired key exists in a certain dictionary you can use an if condition and in the keyword. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } if "kind" in Fruit: print("the desired key exists")
Output:
the desired key exists
Determine dictionary length
To get the length of a dictionary or number of elements(pairs) in it you can use the len() function by passing the dictionary name as a function parameter. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } print(len(Fruit))
Output:
3
Adding an element to the dictionary
You can simply add a new element(key:value)by creating a key index and assign a value to it. By writing the dictionary name followed by square brackets[] where these brackets contain the new key and assigning to it the new, now you created a new element. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } Fruit["new_color"] = "Green" print(Fruit)
Output:
{‘kind’: ‘Banana’, ‘color’: ‘Yellow’, ‘weight’: 0.5, ‘new_color’: ‘Green’}
Removing an element from a dictionary
There are several ways to remove an element from a dictionary. The first way is using the pop() function where the dictionary name is written attached to it the pop() function and passing to it the key of the desired element. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } Fruit.pop("color") print(Fruit)
Output:
{‘kind’: ‘Banana’, ‘weight’: 0.5}
Another way to remove is using popitem() function where it removes the last element inserted in a dictionary and returns it as a tuple ( in versions before 3.7 this function used to remove a random element, not the last one). That can happen by attaching the popitem() function to the dictionary name. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } z = Fruit.popitem() print("the removed element: ",z) print("updated dictionary: ",Fruit)
Output:
the removed element: (‘weight’, 0.5)
updated dictionary: {‘kind’: ‘Banana’, ‘color’: ‘Yellow’}
The third way to remove an element is by using the del keyword. By writing down del keyword followed by the dictionary name and desired element key between square brackets[] you can delete this element. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } del Fruit["weight"] print("updated dictionary: ",Fruit)
Output:
updated dictionary: {‘kind’: ‘Banana’, ‘color’: ‘Yellow’}
Copy a dictionary
There are two ways to copy a dictionary. The first way is using the copy() function by attaching it to the original dictionary and assigning it to a new variable, now you created a new dictionary like the original one. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } new_Fruit = Fruit.copy() print(new_Fruit)
Output:
{‘kind’: ‘Banana’, ‘color’: ‘Yellow’, ‘weight’: 0.5}
Another way to copy a dictionary is by using the dict() constructor. As mentioned in the creating dictionary section you will create a dictionary using the dict() but instead of passing to its elements, simply you will pass to it the name of the original dictionary. Let’s see the syntax:
Code:
Fruit = { "kind": "Banana", "color": "Yellow", "weight": 0.5 } new_Fruit = dict(Fruit) print(new_Fruit)
Output:
{‘kind’: ‘Banana’, ‘color’: ‘Yellow’, ‘weight’: 0.5}
Nested Dictionaries
You can create a dictionary of dictionaries using several methods and that is called nested dictionaries. The first method is by writing all the required dictionaries in a key:value form
Inside the main dictionary. Let’s see the syntax:
Code:
Fruits = { "Fruit_1":{"kind": "Banana", "color": "Yellow", "weight": 0.5} , "Fruit_2":{"kind": "Cherry", "color": "Red", "weight": 0.4} , "Fruit_3":{"kind": "Orange", "color": "Orange", "weight": 0.6} } print(Fruits)
Output:
{‘Fruit_1’: {‘kind’: ‘Banana’, ‘color’: ‘Yellow’, ‘weight’: 0.5}, ‘Fruit_2’: {‘kind’: ‘Cherry’, ‘color’: ‘Red’, ‘weight’: 0.4}, ‘Fruit_3’: {‘kind’: ‘Orange’, ‘color’: ‘Orange’, ‘weight’: 0.6}}
Another method is by creating three different dictionaries and creating another one and passing the three dictionaries as values to it. Let’s see the syntax:
Code:
Fruit_1 = {"kind": "Banana", "color": "Yellow", "weight": 0.5} Fruit_2 = {"kind": "Cherry", "color": "Red", "weight": 0.4} Fruit_3 = {"kind": "Orange", "color": "Orange", "weight": 0.6} Fruits = { "Fruit_1": Fruit_1, "Fruit_2": Fruit_2, "Fruit_3": Fruit_3 } print(Fruits)
Output:
{‘Fruit_1’: {‘kind’: ‘Banana’, ‘color’: ‘Yellow’, ‘weight’: 0.5}, ‘Fruit_2’: {‘kind’: ‘Cherry’, ‘color’: ‘Red’, ‘weight’: 0.4}, ‘Fruit_3’: {‘kind’: ‘Orange’, ‘color’: ‘Orange’, ‘weight’: 0.6}}