Python Dictionaries
Dictionary in Python is a collection of data with key-value pairs which are unordered, unindexed and mutable. This type allows no duplicate keys.
Example
user = {
"first_name":"Danny",
"last_name":"Brown",
"email":"[email protected]"
}
print(user)
Output
Access Dictionary Items
The get() method takes a key as an argument and returns the value which is associated with that key.
Example
user = {
"first_name":"Danny",
"last_name":"Brown",
"email":"[email protected]"
}
first_name = user.get("first_name")
last_name = user.get("last_name")
email = user.get("email")
print(first_name)
print(last_name)
print(email)
Output
You can also access the value of a dictionary by referring the key within the square brackets.
Example
user = {
"first_name":"Danny",
"last_name":"Brown",
"email":"[email protected]"
}
first_name = user["first_name"]
last_name = user["last_name"]
email = user["email"]
print(first_name)
print(last_name)
print(email)
Output
Loop Through Dictionary
You can use for loop to loop through a dictionary in Python.
Example
user = {
"first_name":"Danny",
"last_name":"Brown",
"email":"[email protected]"
}
for i in user.values():
print(i)
Output
Check If a Key exists in Dictionary
You can use the in keyword to check if a key exists in a dictionary.
Example
user = {
"first_name":"Danny",
"last_name":"Brown",
"email":"[email protected]"
}
if "email" in user:
print("Email Key Exists")
Output
Add Item in Dictionary
An item can be added in a dictionary by assigning a new key with value to it.
Example
user = {
"first_name":"Danny",
"last_name":"Brown",
"email":"[email protected]"
}
user["course"] = "Computer"
print(user)
Output
Pop Item from a Dictionary
An item with a specified key can be removed from a dictionary by using the pop() method.
Example
user = {
"first_name":"Danny",
"last_name":"Brown",
"email":"[email protected]"
}
user.pop("last_name")
print(user)
Output
Delete Item from a Dictionary
The del keyword deletes an item with the specified key from a dictionary.
Example
user = {
"first_name":"Danny",
"last_name":"Brown",
"email":"[email protected]"
}
del user["email"]
print(user)
Output
Dictionary Length
The length of a dictionary can be obtained by using the len() function in Python.
Example
user = {
"first_name":"Danny",
"last_name":"Brown",
"email":"[email protected]"
}
print(len(user))
Output
Clear Dictionary
The clear() method clears or empties a dictionary.
Example
user = {
"first_name":"Danny",
"last_name":"Brown",
"email":"[email protected]"
}
user.clear()
print(user)
Output
The dict() Constructor
You can also create a dictionary using the dict() constructor.
Example
customer = dict(name = "Peter", email = "peter@tb.com", customer_id = "ABC123")
print(customer)
Output
Copy Dictionary
The copy() method copies dictionary to another variable.
Example
dict1 = {"name" : "Peter", "email" : "[email protected]", "customer_id" : "ABC123"}
dict2 = dict1.copy()
print(dict2)
Output
Nested Dictionary
Nested Dictionary means dictionary within a dictionary.
Example
dict1 = {
"nested_dict1" : {
"name" : "Peter",
"email" : "[email protected]",
"customer_id" : "ABC123"
},
"nested_dict2" : {
"name" : "Danny",
"email" : "[email protected]",
"customer_id" : "ABC545"
}
}
print(dict1)