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
  • What is a CTE in SQL? SQL
  • how to create charts in Tkinter Python
  • What is an Algorithm? machine learning
  • What is Javascript? Javascript
  • how to update records in SQL CRUD
  • How to sort a Python Dictionary Python
  • ValueError: invalid literal for int() with base 10 Value Error
  • TypeError: List Indices Must Be Integers Or Slices, Not Tuple exceptions

How To Delete a Set of Keys From a Python Dictionary

Posted on January 3, 2023February 12, 2023 By admin

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 Create An Empty Dictionary In Python python dictionaries
  • how do I declare a null value in python? exception handling
  • How to Compare Column Headers in CSV to a List in Python CSV
  • ValueError: Columns must be same length as key exception handling
  • How To Check For Unwanted Characters Using Loops With Python Python Data Cleansing
  • How to sort a Python Dictionary Python

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • Deutsch
  • Python Dictionary Interview Questions Python
  • ValueError: invalid literal for int() with base 10 Value Error
  • How to Create an XML file from Excel using Python Python
  • What are measures in Tableau? data visualisation
  • How to create a combobox in tkinter Python
  • TypeError: ‘float’ object is not callable Python
  • How to show percentage differences between files in Python CSV
  • How to check if a file is empty Python

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