ItSolutionStuff.com

How to Remove Item from Dictionary in Python?

By Hardik Savani • October 30, 2023
Python

Hello Guys,

In this tute, we will discuss how to remove item from dictionary in python. if you want to see an example of python remove item from dictionary then you are in the right place. This post will give you a simple example of how to remove item from python dictionary. This post will give you a simple example of how to remove item from dict python.

There are several ways to remove items from a dictionary in python. i will give you four examples using pop(), popitem(), del and using value in python.

So, without further ado, let's see simple examples:

Example 1: Python Dictionary Remove Item using pop()

main.py

user = {
  "ID": 1,
  "name": "Hardik Savani",
  "email": "hardik@gmail.com"
}
  
# Remove Item from dictionary
user.pop("email")
  
print(user)

Output:

{
 'ID': 1,
 'name': 'Hardik Savani'
}

Example 2: Python Dictionary Remove Item using popitem()

main.py

user = {
  "ID": 1,
  "name": "Hardik Savani",
  "email": "hardik@gmail.com"
}
  
# Remove Item from dictionary
user.popitem()
  
print(user)

Output:

{
 'ID': 1,
 'name': 'Hardik Savani'
}

Example 3: Python Dictionary Remove Item using del

main.py

user = {
  "ID": 1,
  "name": "Hardik Savani",
  "email": "hardik@gmail.com"
}
  
# Remove Item from dictionary
del user["email"]
  
print(user)

Output:

{
 'ID': 1,
 'name': 'Hardik Savani'
}

Example 4: Python Dictionary Remove Item using value

main.py

user = {
  "ID": 1,
  "name": "Hardik Savani",
  "email": "hardik@gmail.com"
}
  
# Remove Item from dictionary
user = {key:val for key, val in user.items() if val != "hardik@gmail.com"}
  
print(user)

Output:

{
 'ID': 1,
 'name': 'Hardik Savani'
}

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Add Element in Dictionary Python?

Read Now →

How to Add Item in Dictionary Python?

Read Now →

Python Create JSON File from Dict Example

Read Now →

Python List Add Element at Beginning Example

Read Now →

How to Add Element at Specific Index in Python List?

Read Now →

How to Convert List to Capitalize First Letter in Python?

Read Now →

How to Get Max Value from Python List?

Read Now →

How to Convert List to Uppercase in Python?

Read Now →

Python Convert List into String with Commas Example

Read Now →

Python Split String into List of Characters Example

Read Now →

How to Convert String into List in Python?

Read Now →

How to Remove Duplicate Values from List in Python?

Read Now →