How to Remove Keys From a Dictionary in Python
Python is an easy-to-read, open-source programming language often introduced as a first language for beginning programmers. A dictionary in Python is a datatype that defines one-to-one relationships between keys and values. If you remove the key from a Python dictionary, you will also remove the key's related value. You use the built-in "del" command to delete a key from a dictionary.
Instructions
-
-
1
Open your Python editor.
-
2
Enter your dictionary data at the command line and press "Enter" to move to a new line. For example, type:
dict = {'server': 'Tardis', 'database' : 'theDoctor', 'application': 'timeTravel' }
-
-
3
Delete the dictionary key at the command line and press "Enter" to move to a new line. For example, type:
del dict ['database']
-
4
Print the updated dictionary at the command line and press "Enter" for Python to output the dictionary data. For example, type:
print dict
Python deletes the key 'database' and its associated value, 'theDoctor', and prints the updated dictionary. For example:
{'application': 'timeTravel', 'server': 'Tardis'}
-
1