Python Dictionary
A dictionary consists of a collection of key-value pairs. It does not allow duplicates. Key is the important part of the dictionary for data manipulation. This section will cover all about Python dictionary.
Objectives
-
Python Dictionary Syntax
-
Create and assign value in Dictionary
-
Python Dictionary Access elements
-
Dictionary data manipulations
-
Python List of Dictionary methods and it’s task
Python Dictionary Syntax
dictionary = {"key" : "value"}
- Here
-
-
Keys are unique within a dictionary while values may not be.
-
The keys must be of an immutable data type such as strings, numbers, or tuples
-
The values of a dictionary can be of any type.
-
Create and assign value in Python Dictionary
# Define a dictionary
string_dictionary = {}
# Assign value to a dictionary
string_dictionary = {
"name": "Touhid Mia",
"country": "Bangladesh",
"age": 28,
}
Python Dictionary Access elements
This part you are going to learn access dictionary elements using various way, let’s jump into the codes.
Common Dictionary
# Declare Dictionary for access data element
dictionary_data = {
"name": "Touhid Mia",
"country": "Bangladesh",
"age": 28,
"height": 5.10,
"isBeard": True,
}
Access element by key
Code
# Access element by key
name = dictionary_data["name"]
print(name)
Output
Touhid Mia
Process finished with exit code 0
Access element by get function
Code
# Access element by get function
name = dictionary_data.get("name")
print(name)
Output
Touhid Mia
Process finished with exit code 0
Access dictionary all keys
Code
# Access dictionary all keys
all_keys = dictionary_data.keys()
print(all_keys)
Output
dict_keys(['name', 'country', 'age', 'height', 'isBeard'])
Process finished with exit code 0
Access dictionary all values
Code
# Access dictionary all values
all_keys = dictionary_data.values()
print(all_keys)
Output
dict_values(['Touhid Mia', 'Bangladesh', 28, 5.1, True])
Process finished with exit code 0
Access dictionary items as tuples of list
Code
# Access dictionary items as tuples of list
list_of_tuples = dictionary_data.items()
print(list_of_tuples)
Output
dict_items([('name', 'Touhid Mia'), ('country', 'Bangladesh'), ('age', 28), ('height', 5.1), ('isBeard', True)])
Process finished with exit code 0
Check dictionary key is exist on it
Code
# Check dictionary key is exist on it
if "name" in dictionary_data:
print("Yes key exist")
else:
print("Key not exist")
Output
Yes key exist
Process finished with exit code 0
Print each element of dictionary by loop
Code
# Print each element of dictionary by loop
for key in dictionary_data:
value = dictionary_data[key] # Access value using key
print("Key : " + key + ", Value : " + str(value))
Output
Key : name, Value : Touhid Mia
Key : country, Value : Bangladesh
Key : age, Value : 28
Key : height, Value : 5.1
Key : isBeard, Value : True
Process finished with exit code 0
Python Dictionary data manipulations
Earlier part you saw that how to retrieve or access element from a dictionary, this part you learn how to add, remove, update data to dictionary
Common Dictionary
# Declare Dictionary for access data element
dictionary_data = {
"name": "Touhid Mia",
"country": "Bangladesh",
"age": 28,
"height": 5.10,
"isBeard": True,
}
Dictionary add item using key
Code
# Dictionary add item using key
dictionary_data["isMarried"] = True
print(dictionary_data)
Output
{'name': 'Touhid Mia', 'country': 'Bangladesh', 'age': 28, 'height': 5.1, 'isBeard': True, 'isMarried': True}
Process finished with exit code 0
Dictionary add item using update function
Code
# Dictionary add item using update function
dictionary_data.update({"hasChild": True})
print(dictionary_data)
Output
{'name': 'Touhid Mia', 'country': 'Bangladesh', 'age': 28, 'height': 5.1, 'isBeard': True, 'isMarried': False, 'hasChild': True}
Process finished with exit code 0
Dictionary update item using key
Code
# Dictionary update item using key
dictionary_data["age"] = 30
print(dictionary_data)
Output
{'name': 'Touhid Mia', 'country': 'Canada', 'age': 30, 'height': 5.1, 'isBeard': True, 'isMarried': True, 'hasChild': True}
Process finished with exit code 0
Dictionary update item using update function
Code
# Dictionary update item using update function
dictionary_data.update({"country": "Canada"})
print(dictionary_data)
Output
{'name': 'Touhid Mia', 'country': 'Canada', 'age': 30, 'height': 5.1, 'isBeard': True, 'isMarried': True, 'hasChild': True}
Process finished with exit code 0
Dictionary remove item using key and pop function
Code
# Dictionary remove item using key and pop function
dictionary_data.pop("age")
print(dictionary_data)
Output
{'name': 'Touhid Mia', 'country': 'Canada', 'height': 5.1, 'isBeard': True, 'isMarried': True, 'hasChild': True}
Process finished with exit code 0
Dictionary remove item using key and del
Code
# Dictionary remove item using key and del
del dictionary_data["country"]
print(dictionary_data)
Output
{'name': 'Touhid Mia', 'height': 5.1, 'isBeard': True, 'isMarried': True, 'hasChild': True}
Process finished with exit code 0
Dictionary remove last inserted item by popitem function
Code
# Dictionary remove last inserted item by popitem function
dictionary_data.popitem()
print(dictionary_data)
Output
{'name': 'Touhid Mia', 'height': 5.1, 'isBeard': True, 'isMarried': True}
Process finished with exit code 0
Dictionary remove all item using clear function
Code
# Dictionary remove all item using clear function
dictionary_data.clear()
print(dictionary_data)
Output
{}
Process finished with exit code 0
Python List of Dictionary methods and it’s task
-
keys() : Returns a list containing the dictionary’s keys
-
values() : Returns a list of all the values in the dictionary
-
get() : Returns the value of the specified key
-
items() : Returns a list containing a tuple for each key value pair
-
clear() : Removes all the elements from the dictionary
-
copy() : Returns a copy of the dictionary
-
fromkeys() : Returns a dictionary with the specified keys and value
-
pop() : Removes the element with the specified key
-
popitem() : Removes the last inserted key-value pair
-
setdefault() : Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
-
update() : Updates the dictionary with the specified key-value pairs