[Tutor] replacing a long list of if,elif

Emile van Sebille emile at fenx.com
Fri Oct 23 18:25:41 CEST 2009


>> myDict.get(fldType, None)
> 
>> this will output the value from myDict if the key exists, if not it will
>>
>> return None 
> 
> Thanks this looks perfect - did not know dict.get()

You may also want to consider {}.setdefault -- that would collect the 
fldType entries not defined.

 >>> a = {'a':1,'b':2}
 >>> a.setdefault('a','?')
1
 >>> a.setdefault('c','?')
'?'
 >>> a
{'a': 1, 'c': '?', 'b': 2}

Emile



More information about the Tutor mailing list