How to Remove Key from Dictionary in Python

‘Key’ in Dictionary

In Python, a dictionary is a collection of key-value pairs. A key in a Python dictionary is a unique identifier that is used to retrieve its associated value.

Keys in a dictionary must be unique, immutable, and hashable objects. This means that they cannot be changed once they are created, and they must be of a type that has a hash function implemented, such as strings, numbers, and tuples of immutable objects.

For example, consider the following dictionary:

Python
dict1 = {'tiger': 11, 'lion': 22, 'monkey': 33}

In this dictionary, 'tiger', 'lion', and 'monkey' are the keys, and ’11’,’ 22′, and ’33’ are the corresponding values. You can use the keys to access the associated values in the dictionary, like this:

Python
print(dict1['tiger'])  
print(dict1['monkey'])  

Output:

11
33

Remove Key from Dictionary

To delete a key from a Python dictionary, you can use the ‘del‘ statement followed by the dictionary name and the key that you want to delete. Here’s an example:

Python
my_dict = {'tiger': 1, 'lion': 2, 'monkey': 3}
del my_dict['lion']
print(my_dict) 

Output:

{'tiger': 1, 'monkey': 3}

In this example, the ‘del‘ statement removes the key 'lion' and its associated value 2 from the my_dict dictionary. The resulting dictionary is {'tiger': 1, 'monkey': 3}.

Storing data in a dictionary

To store data in a dictionary, you can create an empty dictionary and add key-value pairs to it using the square bracket notation. Here’s an example:

Python
# Create an empty dictionary
my_dict = {}

# Add key-value pairs to the dictionary
my_dict['name'] = 'Alice'
my_dict['age'] = 30
my_dict['city'] = 'New York'

# Print the dictionary
print(my_dict)

In this example, we start with an empty dictionary ‘my_dict‘. We then add three key-value pairs to the dictionary, where the keys are 'name', 'age', and 'city', and the corresponding values are 'Alice', 30, and 'New York'. Finally, we print the dictionary using the ‘print()‘ function. The output of this code will be:

{'name': 'Alice', 'age': 30, 'city': 'New York'}

Retrieving data from a dictionary

To retrieve data from a dictionary, you can use the square bracket notation with the key of the value you want to retrieve. Here’s an example:

Python
# Create a dictionary
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}

# Retrieve data from the dictionary
print(my_dict['name'])
print(my_dict['age'])
print(my_dict['city'])

In this example, we start with a dictionary ‘my_dict‘ that contains three key-value pairs. We then use the square bracket notation to retrieve the values associated with the keys 'name', 'age', and 'city' and print them using the ‘print()‘ function. The output of this code will be:

Alice
30
New York

If you try to retrieve a key that does not exist in the dictionary, a ‘KeyError‘ exception will be raised. To avoid this, you can use the ‘.get()‘ method instead of the square bracket notation, which returns ‘None‘ if the key is not found, or a default value that you can specify. For example:

Python
# Retrieve data from the dictionary using .get()
print(my_dict.get('name'))
print(my_dict.get('email'))  # This key does not exist, so None will be returned
print(my_dict.get('phone', 'Unknown'))  # This key does not exist, so 'Unknown' will be returned

In this example, we use the ‘.get()‘ method to retrieve the values associated with the keys 'name', 'email', and 'phone'. The first call to ‘.get()‘ will return 'Alice', the second call will return ‘None‘, and the third call will return 'Unknown', because we specified a default value of 'Unknown' for the 'phone' key.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.