How to Delete Item by Key from Python Dictionary?

30-Jan-2023

.

Admin

How to Delete Item by Key from Python Dictionary?

Hi Dev,

This is a short guide on how to delete item by key from python dictionary. This post will give you simple example of python dictionary remove entry by key. we will help you to give example of python dictionary remove element by key. step by step explain python dictionary remove item by index. Let's see bellow example python remove items from dictionary by key.

There are several ways to remove element by index from a dictionary in python. i will give you four examples using pop() and del in python.

Example 1: Python Dictionary Remove Element using pop()


main.py

user = {

"ID": 1,

"name": "Piyush Kamani",

"email": "piyush@gmail.com"

}

# Remove Item from dictionary

user.pop("email")

print(user)

Output:

{

'ID': 1,

'name': 'Piyush Kamani'

}

Example 2: Python Dictionary Remove Element using del

main.py

user = {

"ID": 1,

"name": "Piyush Kamani",

"email": "piyush@gmail.com"

}

# Remove Item from dictionary

del user["email"]

print(user)

Output:

{

'ID': 1,

'name': 'Piyush Kamani'

}

#Python