Deleting Elements using ‘del’ Keyword
The items of the dictionary can be deleted by using the del keyword as given below.
Example: The code defines a dictionary, prints its original content, and then uses the ‘del’ statement to delete the element associated with key 1. After deletion, it prints the updated dictionary, showing that the specified element has been removed.
[GFGTABS]
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
print("Dictionary =")
print(Dict)
del(Dict[1])
print("Data after deletion Dictionary=")
print(Dict)
[/GFGTABS]
Output
Dictionary ={1: 'Geeks', 'name': 'For', 3: 'Geeks'}
Data after deletion Dictionary={'name': 'For', 3: 'Geeks'}
Dictionaries in Python
[GFGTABS]
dict = {
1: 'Python',
2: 'dictionary',
3: 'example'
}
[/GFGTABS]
A Python dictionary is a data structure that stores the value in key:value pairs.
Example:
As you can see from the example, data is stored in key:value pairs in dictionaries, which makes it easier to find values.
[GFGTABS]
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print(Dict)
[/GFGTABS]
Output:
{1: 'Geeks', 2: 'For', 3: 'Geeks'}