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]
Python

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]
Python

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]
Python

Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print(Dict)


[/GFGTABS]

Output:

{1: 'Geeks', 2: 'For', 3: 'Geeks'}

Similar Reads

Python Dictionary Syntax

dict_var = {key1 : value1, key2 : value2, …..}...

What is a Dictionary in Python?

Dictionaries in Python is a data structure, used to store values in key:value format. This makes it different from lists, tuples, and arrays as in a dictionary each key has an associated value....

Dictionary Example

A dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing curly braces{}....

Nested Dictionaries

...

Adding Elements to a Dictionary

The addition of elements can be done in multiple ways. One value at a time can be added to a Dictionary by defining value along with the key e.g. Dict[Key] = ‘Value’....

Accessing Elements of a Dictionary

To access the items of a dictionary refer to its key name. Key can be used inside square brackets....

Accessing an Element of a Nested Dictionary

To access the value of any key in the nested dictionary, use indexing [] syntax....

Deleting Elements using ‘del’ Keyword

The items of the dictionary can be deleted by using the del keyword as given below....

Dictionary Methods

Here is a list of in-built dictionary functions with their description. You can use these functions to operate on a dictionary....