Bottom Page

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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
Quote
#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'.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
Quote
#5
For me, the fastest way is
c = d.copy()
c.pop('bar')
(tested with the timeit module)
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pass results of expression to another expression cmdr_eggplant 2 162 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  curious syntax with dictionary item inselbuch 3 616 Mar-09-2019, 04:21 PM
Last Post: ichabod801
  deleting item from dictionary Skaperen 6 674 Feb-20-2019, 01:18 AM
Last Post: Skaperen
  Reversing word in strings yields unexpected result Dec 4 1,403 May-17-2017, 05:32 PM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)