You may supply your own "default" value : Fromkeys : Dictionary PYTHON TUTORIALS


PYTHON TUTORIALS » Dictionary » Fromkeys »

 

You may supply your own "default" value


d = dict.fromkeys(['name', 'age'], '(unknown)') 
d.get('name', 'N/A') 

If the key is there, get works like ordinary dictionary lookup: 

d = dict.fromkeys(['name', 'age'], '(unknown)') 

d['name'] 'Eric
print d.get('name') 

ddict = {}.fromkeys(('x''y'), -1)
print ddict
edict = {}.fromkeys(('foo', 'bar'))
print edict



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Dictionary
» Fromkeys