How to delete a key from a Python dictionary

Estimated reading time: 2 minutes

This is an addition to the list of questions you may get when in an interview and you are asked to give an overview of Python.

As with how to create an empty dictionary in Python, how to add values to a python dictionary, and how to sort a python dictionary we will take you through the steps here of how to delete a key from a python dictionary.

How to delete a key from a Python dictionary.

How to use the pop() method to delete a key from a dictionary

In the below example we tell Python to find the key “Key1”, then when it does it prints the python dictionary without that key or its value.

empty_dict1 = {}

empty_dict1['Key2'] = '2'
empty_dict1['Key1'] = '1'
empty_dict1['Key3'] = '3'
print(empty_dict1)

#1. Use the pop function
empty_dict1.pop('Key1')
print(empty_dict1)

Result:
{'Key2': '2', 'Key3': '3'}

How to use the Del keyword to delete a key from a dictionary

In this example, we are taking the output of the above example, and just telling the logic to remove the key “Key2” then what it does is it prints the python dictionary without that key or its value.

del empty_dict1["Key2"]
print(empty_dict1)

Result:
{'Key3': '3'}

How to use dict.clear() to delete a key from a dictionary

In this final example, we use dict.clear(). Note this will empty everything out of the dictionary, so be careful in its use.

As can be seen, it takes the output of the previous example and empties it completely.

empty_dict1.clear() # Removes everything from the dictionary.
print(empty_dict1)

Result:
{}

How To Fix TypeError: unhashable type ‘slice’

Estimated reading time: 2 minutes

So you have a Python dictionary, and you want to retrieve data from it and print it to a screen. There are some characteristics of Python that first of all should be understood:

  1. They are mutable
  2. They also have the ability to grow and shrink as required.
  3. Data is accessed within the dictionary via keys.

The last point is very important as dictionaries do not have an index value, and this is why you get the TypeError you are here to solve for.

So let us recreate the problem

In the below code we have a dictionary called “userdata”, with its key-value pairs.

The loop is trying to retrieve the index value 1 for all the values in dai_data.

As can be seen, dai_data is trying to retrieve the last three index values within the dictionary.

As noted above the only way to access dictionary values is through their key values.

userdata = {
	  "name": "Data Analytics Ireland",
	  "Country": "Ireland",
	  "City": "Dublin",
	  "Age": "1000 years!",
}

dai_data = userdata[:3]

for i in dai_data:
	     print(i[1])

Output:
Traceback (most recent call last):
  File "", line 8, in <module>
    dai_data = userdata[:3]
TypeError: unhashable type: 'slice'

So how do we fix this problem?

First of all, values are accessed through the key within the dictionary

In the below dictionary the key values are: Name, Country, City, Age

userdata = {
	  "Name": "Data Analytics Ireland",
	  "Country": "Ireland",
	  "City": "Dublin",
	  "Age": "1000 years!",
}

print(userdata["Name"])
print(userdata["Country"])
print(userdata["City"])
print(userdata["Age"])

Output:
Data Analytics Ireland
Ireland
Dublin
1000 years!

As a result, now we are able to access the values associated with each key.

Did you know you could add a list to one of your key-value pairs?

In the above example, we focused on a single value, but we could also make a key equal to a list.

userdata = {
	  "Name": ["Joe","Jim"],
	  "Country": "Ireland",
	  "City": ["Dublin","Cork","Limerick"],
	  "Age": "1000 years!"
}

print(userdata["Name"])
print(userdata["Country"])
print(userdata["City"])
print(userdata["Age"])

Output:
['Joe', 'Jim']
Ireland
['Dublin', 'Cork', 'Limerick']
1000 years!