0

For a python dictionary is it possible to create a key that the dictionary will default to if the requested key is not present ?

Edit: I fail to understand how the solutions pointed below and above address the question

If I ask for dictionary['xxx'] where xxx is not a known value or a variable , it can be any string, how can I use dictionary['key'] and dictionary.get('key','defaultvalue')

Edit2:

spouse={John:Joan, Bob:Marry}

when I ask for spouse[Dan] I should get "not married" same should go for any male mane that comes to user's mind and it is not a key in the dictionary

I hope that now it is clearer

The defaultdict comment seems to be the only useful

MiniMe
  • 1,057
  • 4
  • 22
  • 47
  • 6
    `defaultdict` to the rescue! – Ry- Apr 10 '17 at 13:40
  • 3
    Or [`dict.get`](https://docs.python.org/3/library/stdtypes.html#dict.get), depending on whether accessing non-existent keys should mutate the dictionary or not. – mkrieger1 Apr 10 '17 at 13:43
  • 1
    Would providing a default value to the `dict.get` suit your needs? `my_dictionary.get('key1', 'this_is_the_value_returned_if_key1_is_not_in_the_dict')`. – John Apr 10 '17 at 13:44
  • Guys ..do I need to say that the string used as key is unknown? – MiniMe Apr 10 '17 at 15:27
  • If neither of the answers addresses your question, can you post some actual code you are using? That would make it easier to understand what you want to achieve exactly. – mkrieger1 Apr 10 '17 at 16:08
  • just updated, please see above – MiniMe Apr 10 '17 at 17:43
  • Solved the problem with defaultdict ! Many thanks to Ryan – MiniMe Apr 10 '17 at 18:47
  • `spouse.get('Dan', 'not married')` would be the simplest solution, meaning "get me the spouse of Dan if there is one, or 'not married' otherwise". – mkrieger1 Apr 10 '17 at 21:33
  • suppose you do not know the key string in advance. defaultdict was defined specifically for this kind of situation and it works as expected – MiniMe Apr 10 '17 at 23:31

1 Answers1

0

When calling a dictionary that may or may not have a given key you can set a default like this:

>>> my_dict = {'color':'red', 'size':'2'}
>>> my_dict.setdefault('style', 'round')
'round'

>>> my_dict = {'color':'red', 'size':'2', 'style':'square'}
>>> my_dict.setdefault('style', 'round')
'square'

regarding your edited question:

Edit2:

spouse={John:Joan, Bob:Marry}

when I ask for spouse[Dan] I should get "not married" same should go for any male name that comes to user's mind and it is not a key in the dictionary

You could accomplish that like this:

>>> spouse = {}
>>> spouse['Jim']='Lucy'
>>> spouse['Alex']='Sandra'
>>> users = ['Dan', 'Phil','Jim','Alex']
>>> for i in range(len(users)):
>>>     s = spouse.setdefault(users[i], 0)
>>>     if s == 0:
>>>         print "%s is not married" % users[i]
>>>     else:
>>>         print "%s's spouse is %s" % (users[i],s)

'Dan is not married'
'Phil is not married'
'Jim's spouse is Lucy'
'Alex's spouse is Sandra'
litepresence
  • 3,109
  • 1
  • 27
  • 35