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":"danny@tutorialsbuddy.com"   
}
print(user)
Output
{'first_name': 'Danny', 'last_name': 'Brown', 'email': 'danny@tutorialsbuddy.com'}

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":"danny@tutorialsbuddy.com"
}

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
Danny
Brown
danny@tutorialsbuddy.com

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":"danny@tutorialsbuddy.com"
}

first_name = user["first_name"]
last_name = user["last_name"]
email = user["email"]

print(first_name)
print(last_name)
print(email)
  
Output
Danny
Brown
danny@tutorialsbuddy.com

Loop Through Dictionary

You can use for loop to loop through a dictionary in Python.

Example

user = {
"first_name":"Danny",
"last_name":"Brown",
"email":"danny@tutorialsbuddy.com"
}

for i in user.values():
    print(i)
  
Output
Danny
Brown
danny@tutorialsbuddy.com

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":"danny@tutorialsbuddy.com"
}

if "email" in user:
    print("Email Key Exists")
  
Output
Email Key Exists

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":"danny@tutorialsbuddy.com"
}
        
user["course"] = "Computer"
print(user)
  
Output
{'first_name': 'Danny', 'last_name': 'Brown', 'email': 'danny@tutorialsbuddy.com', 'course': 'Computer'}

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":"danny@tutorialsbuddy.com"
}
            
user.pop("last_name")
print(user)
  
Output
{'first_name': 'Danny', 'email': 'danny@tutorialsbuddy.com'}

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":"danny@tutorialsbuddy.com"
}

del user["email"]
print(user)
  
Output
{'first_name': 'Danny', 'last_name': 'Brown'}

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":"danny@tutorialsbuddy.com"
}
print(len(user))
  
Output
3

Clear Dictionary

The clear() method clears or empties a dictionary.

Example

user = {
"first_name":"Danny",
"last_name":"Brown",
"email":"danny@tutorialsbuddy.com"
}
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
{'name': 'Peter', 'email': 'peter@tb.com', 'customer_id': 'ABC123'}

Copy Dictionary

The copy() method copies dictionary to another variable.

Example

dict1 = {"name" : "Peter", "email" : "peter@tb.com", "customer_id" : "ABC123"}
dict2 = dict1.copy()
print(dict2)
  
Output
{'name': 'Peter', 'email': 'peter@tb.com', 'customer_id': 'ABC123'}

Nested Dictionary

Nested Dictionary means dictionary within a dictionary.

Example

dict1 = {
"nested_dict1" : {
    "name" : "Peter",
    "email" : "peter@tb.com",
    "customer_id" : "ABC123"
    },
"nested_dict2" : {
    "name" : "Danny",
    "email" : "danny@tb.com",
    "customer_id" : "ABC545"
    }
}

print(dict1)
  
Output
{'nested_dict1': {'name': 'Peter', 'email': 'peter@tb.com', 'customer_id': 'ABC123'}, 'nested_dict2': {'name': 'Danny', 'email': 'danny@tb.com', 'customer_id': 'ABC545'}}