Python Dictionary Delete Item by Key Example
Hi Friends,
This is a short guide on python dictionary remove item by key. We will look at an example of python dictionary delete item by key. Here you will learn python remove items from dictionary by key. We will look at an example of python dictionary remove item by index. Alright, let us dive into the details.
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.
So, without further ado, let's see simple examples:
Example 1: Python Dictionary Remove Element 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 Element 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' }
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Add a Key Value Pair to Dictionary in Python?
- How to Add Element in Dictionary Python?
- How to Add Item in Dictionary Python?
- Python Openpyxl Read Excel File Example
- Python Read Excel File with Multiple Sheets Example
- Python Create JSON File from Dict Example
- How to Add Multiple Elements to a List in Python?
- How to Convert List to Uppercase in Python?
- Python Convert List into String with Commas Example
- How to Convert List into String in Python?
- How to Convert String into List in Python?
- How to Remove Duplicate Values from List in Python?
- How to Remove null Values from the List in Python?