Remove a dictionary key that has a certain value [duplicate]

2024/9/30 7:15:42

I know dictionary's are not meant to be used this way, so there is no built in function to help do this, but I need to delete every entry in my dictionary that has a specific value.

so if my dictionary looks like:

'NameofEntry1': '0'
'NameofEntry2': 'DNC'...

I need to delete(probably pop) all the entries that have value DNC, there are multiple in the dictionary.

Answer

Modifying the original dict:

for k,v in your_dict.items():if v == 'DNC':del your_dict[k]

or create a new dict using dict comprehension:

your_dict = {k:v for k,v in your_dict.items() if v != 'DNC'}

From the docs on iteritems(),iterkeys() and itervalues():

Using iteritems(), iterkeys() or itervalues() while adding ordeleting entries in the dictionary may raise a RuntimeError or failto iterate over all entries.

Same applies to the normal for key in dict: loop.

In Python 3 this is applicable to dict.keys(), dict.values() and dict.items().

https://en.xdnf.cn/q/71111.html

Related Q&A

Get names of positional arguments from functions signature

Using Python 3.x, Im trying to get the name of all positional arguments from some function i.e: def foo(a, b, c=1):returnRight now Im doing this: from inspect import signature, _empty args =[x for x, p…

Emacs Python-mode syntax highlighting

I have GNU Emacs 23 (package emacs23) installed on an Ubuntu 10.04 desktop machine and package emacs23-nox installed on an Ubuntu 10.04 headless server (no X installed). Both installations have the sam…

programmatically edit tab order in pyqt4 python

I have a multiple textfield in my form. My problem is the tab order is wrong. Is there a way to edit tab order in code? Just like in QT Designer.thanks.

Getting the parent of a parent widget in Python Tkinter

I am trying to get the parent of a widget then get the parent of that widget. But Everytime I try to I get a error.Error:AttributeError: str object has no attribute _nametowidgetWhy is it giving me tha…

Python Random List Comprehension

I have a list similar to:[1 2 1 4 5 2 3 2 4 5 3 1 4 2] I want to create a list of x random elements from this list where none of the chosen elements are the same. The difficult part is that I would lik…

How to handle unseen categorical values in test data set using python?

Suppose I have location feature. In train data set its unique values are NewYork, Chicago. But in test set it has NewYork, Chicago, London. So while creating one hot encoding how to ignore London? In…

How to get Facebook access token using Python library?

Ive found this Library it seems it is the official one, then found this, but everytime i find an answer the half of it are links to Facebook API Documentation which talks about Javascript or PHP and ho…

How to convert shapefile/geojson to hexagons using uber h3 in python?

I want to create hexagons on my geographic map and want to preserve the digital boundary specified by the shapefile/geojson as well. How do I do it using ubers h3 python library? Im new to shapefiles…

Python mypy: float and int are incompatible types with numbers.Real

I am new to Pythons static typing module mypy. I am trying to append ints and floats to an array, which I typed statically to be Real. But mypy says that they are incompatible types with Real. I though…

Search/Find functionality in QTableView

I have a QWidget and inside that, there is a QTableView. I need to have a find functionality on the first column of the table, so when I click on Ctrl+F, a find dialog will pop-up.class Widget(QWidget)…