Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • TypeError: type object is not subscriptable strings
  • ValueError: invalid literal for int() with base 10 Value Error
  • What is the r programming language R Programming
  • TypeError: ‘NoneType’ object is not iterable class
  • How To Join Tables In SQL SQL
  • How do I fix TypeError: unhashable type: ‘list’ Error? Articles
  • TypeError: ‘float’ object is not callable Python
  • TypeError: cannot unpack non-iterable int object Python

How To Delete a Set of Keys From a Python Dictionary

Posted on January 3, 2023January 14, 2023 By admin No Comments on How To Delete a Set of Keys From a Python Dictionary

Estimated reading time: 2 minutes

In our last video on how to delete a key from a python dictionary, we illustrated where one key could be removed easily.

But what if you wanted to remove one or more keys?

How to delete more than one key from a Python dictionary

In the below code, we have created an empty dictionary. This will be populated by values in “dictionary_remove”.

Option1 uses the pop method :

  1. It creates an empty dictionary and then populates it with keys and values.
  2. Then it uses a loop to iterate over the list dictionary_remove.
  3. Then it looks at those and uses the pop method to find those values in the empty_dict1, and removes them

As a result, the following is what is returned:

Before:

{‘Key2’: ‘2’, ‘Key1’: ‘1’, ‘Key3’: ‘3’, ‘Key4’: ‘4’, ‘Key5’: ‘5’, ‘Key6’: ‘6’}

After:

{‘Key2’: ‘2’, ‘Key1’: ‘1’, ‘Key3’: ‘3’, ‘Key4’: ‘4’}

Option 2 uses the Del method :

In this second scenario, we do the following steps:

  1. We populate the dictionary_remove with two values, that are from the result of scenario 1 above.
  2. Then it uses a loop to iterate over the list dictionary_remove.
  3. Then it looks at those and uses the del method to find those values in the empty_dict1, and removes them

As a result, the following is what is returned:

Before:

{‘Key2’: ‘2’, ‘Key1’: ‘1’, ‘Key3’: ‘3’, ‘Key4’: ‘4’}

After:

{‘Key2’: ‘2’, ‘Key1’: ‘1’}

Slide 8
#How to delete more than one key from a dictionary
#1. Create a list to lookup against
empty_dict1 = {}

empty_dict1['Key2'] = '2'
empty_dict1['Key1'] = '1'
empty_dict1['Key3'] = '3'
empty_dict1['Key4'] = '4'
empty_dict1['Key5'] = '5'
empty_dict1['Key6'] = '6'

print(empty_dict1)

dictionary_remove = ["Key5","Key6"] # Lookup list

#1. Use the pop method

for key in dictionary_remove:
  empty_dict1.pop(key)
print(empty_dict1)

#2 Use the del method
dictionary_remove = ["Key3","Key4"]
for key in dictionary_remove:
  del empty_dict1[key]
print(empty_dict1)

We hope you enjoyed this, we have plenty of videos that you can look at to improve your knowledge of Python here: Data Analytics Ireland Youtube

Click here if you want to know how would you change the name of a key in a python dictionary as an alternative!

python dictionaries, Python Lists, search Tags:Python, python dictionaries

Post navigation

Previous Post: How to delete a key from a Python dictionary
Next Post: How Would You Change The Name Of a Key in a Python Dictionary

Related Posts

  • How To Check For Unwanted Characters Using Loops With Python Python Data Cleansing
  • how do I declare a null value in python? exception handling
  • YouTube channel lists – Python Lists Python Lists
  • ValueError: Columns must be same length as key exception handling
  • How To Add Values to a Python Dictionary Python
  • How to Compare Column Headers in CSV to a List in Python CSV

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • how to build a machine learning model machine learning
  • Python Tutorial: Add a column to a data frame Python Dataframe
  • What is data profiling and its benefits? data profiling
  • Python Tutorial: How to validate data using tuples Python Tuples
  • YouTube channel lists – Python working with files Python working with files
  • How To Add Values to a Python Dictionary Python
  • how to copy/paste special a range of cells with xlwings Python
  • Python Overview Interview Questions automation

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT