Python Forum
looking fo an expression that yields a dictionary without an item
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looking fo an expression that yields a dictionary without an item
#1
i am wanting to make an expression (method call?) that given a dictionary (or set or frozenset), and a hashable value, makes a shallow copy of the dictionary (or set or frozenset) without the item having a key of the hashable value, if one exists in the original unchanged dictionary (or set or frozenset). i only need to do this with a dictionary but i can see potential equivalency for a set or frozenset. at first i wrote d.copy().some_method() but i could not find a method that returned a reference to the dictionary, much less delete an item. i need to do separate steps for this (maybe in a function). the cases i have are difficult to either do separate steps or def a function.

this is another example where if we could add methods to existing builtin classes like dict, it would be an easier solution.

could this be done as a dictionary view? my use case may be able to use that.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
do you mean something like this:

>>> example_dict = {
   'field1': 'value1',
   'complex_field': {
       'part1': 'stuff',
       'part2': 'more stuff'
   }
}
>>> cmplx = example_dict['complex_field']
>>> cmplx['part2']
'more stuff'
>>>
cmplx is essentially a (in C terminology) pointer to the complex_field cell of the dictionary, which
can be used as if it were a separate dictionary.
Reply
#3
this seems to work:
Output:
>>> d={'foo':1,'bar':2} >>> v='bar' >>> dict([x for x in d.items() if x[0] != v]) {'foo': 1}

no, nothing meant about 'complex_field'.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
If dictionary is large then you can save memory by using generator:

>>> dict(x for x in d.items() if x[0] != v)
I tried to understand problem you are solving:
  • given a dictionary (or set or frozenset)
  • a hashable value
  • a shallow copy of the dictionary (or set or frozenset)
  • without the item having a key of the hashable value

Dictionary keys must be hashable, so mentioning hashability seems irrelevant.

Is it complicated way to say that you want to have 'new dictionary where specific keys of original dictionary are omitted' Smile ?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
For me, the fastest way is
c = d.copy()
c.pop('bar')
(tested with the timeit module)
Reply
#6
if i didn't mention hashability, i'm sure someone would inform me that it won't work for all possible values.

i was trying to get this into an expression.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove an item from a list contained in another item in python CompleteNewb 19 5,550 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  How to modify item in dictionary? Winfried 7 3,387 Nov-21-2020, 07:12 PM
Last Post: bowlofred
  python --version yields no output, not detected by other programs ten 3 3,366 Jun-25-2020, 04:48 AM
Last Post: perfringo
  Pass results of expression to another expression cmdr_eggplant 2 2,238 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  curious syntax with dictionary item inselbuch 3 2,725 Mar-09-2019, 04:21 PM
Last Post: ichabod801
  deleting item from dictionary Skaperen 6 3,005 Feb-20-2019, 01:18 AM
Last Post: Skaperen
  Reversing word in strings yields unexpected result Dec 4 3,597 May-17-2017, 05:32 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020