how to safely extract dict values

Bruno Desthuilliers onurb at xiludom.gro
Mon Jul 31 06:31:19 EDT 2006


Paul Rubin wrote:
> David Zaret <dave at zaret.com> writes:
>> my way is ugly.  what's a better way?
> 
> Untested:
> 
>     for key in mydict:
>        if isinstance(mydict[key], list):
>           vals.extend(mydict[key])
>        else:
>           vals.append(mydict[key])

Too much useless lookup IMHO...

vals = []
for k, v in mydict.items():
  if isinstance(v, list):
    vals.extend(v)
  else:
    vals.append(v)

!-)

But this is not that different from what the OP found so ugly...

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list