Python Forum
a dictionary with a key default
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a dictionary with a key default
#1
i can use the method .get() on a dictionary to use a single specific default. i would like to have a simple means like that which can be inserted into an expression which will return the value of the key itself if there is no matching key in the dictionary. i do not want something that requires expressing the key twice, so i can use costly key expressions and/or key expressions with side effects.
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
express the key on separate line and assign to name, then use that name twice in get() call or define custom class that inherit from dict but override get() method and use it instead of built in class
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
More often that not I have trouble grasping the question. There is side effect of adding new key value pair but anyways:

>>> key = 'oh my'                                                   
>>> d = {'meaning of life': 42}                                     
>>> d.setdefault(key, key)                                          
'oh my'
>>> d.setdefault('meaning of life', 'no way')                       
42
>>> d                                                               
{'meaning of life': 42, 'oh my': 'oh my'}
With another side effect:

>>> key = 'oh no'                                                   
>>> d = {'meaning of life': 42}                                     
>>> d.pop(d.setdefault(key, key))                                   
'oh no'
>>> d                                                               
{'meaning of life': 42}
>>> d.pop(d.setdefault('meaning of life', 'oh no'))                 
--------------------------------------------------------------------------
/.../
----> 1 d.pop(d.setdefault('meaning of life', 'oh no'))
KeyError: 42
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
#4
i was wanting to put this as an expression inside a comprehension.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
the dictionary is mapping some aliases for some strings used in some comprehensions.
Tradition is peer pressure from dead people

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


Forum Jump:

User Panel Messages

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