Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to safely extract dict values

Reply
Thread Tools

how to safely extract dict values

 
 
David Zaret
Guest
Posts: n/a
 
      07-31-2006
i have a dict with a particular key - the values for this key will be
None, one valid scalar, or a list:

{mykey, None}
{mykey, "foo"}
{mykey, ["bar", "baz"]}

let's ignore the None case - in the case of the one or many values, i
want to suck the values into a list. here's one way to do this:

if mydict.has_key(mykey):
vals=[]
_v = mydict[mykey]
if isinstance(_v, types.ListType):
vals.extend(_v)
else:
vals.append(_v)
# now we can safely iterate through acts
for val in vals:
.....


my way is ugly. what's a better way?

thanks,

---- dz
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      07-31-2006
David Zaret <(E-Mail Removed)> 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])
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-31-2006
Paul Rubin wrote:
> David Zaret <(E-Mail Removed)> 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 '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
Amit Khemka
Guest
Posts: n/a
 
      07-31-2006
On 7/31/06, David Zaret <(E-Mail Removed)> wrote:
> i have a dict with a particular key - the values for this key will be
> None, one valid scalar, or a list:
>
> {mykey, None}
> {mykey, "foo"}
> {mykey, ["bar", "baz"]}
>
> let's ignore the None case - in the case of the one or many values, i
> want to suck the values into a list. here's one way to do this:
>
> if mydict.has_key(mykey):
> vals=[]
> _v = mydict[mykey]
> if isinstance(_v, types.ListType):
> vals.extend(_v)
> else:
> vals.append(_v)
> # now we can safely iterate through acts
> for val in vals:
> .....


how about:

vals = []
for val in mydict.values():
try: vals.extend(val)
except: vals.append(val)

cheers,
amit.



--
----
Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-31-2006
David Zaret wrote:
> i have a dict with a particular key - the values for this key will be
> None, one valid scalar, or a list:
>
> {mykey, None}
> {mykey, "foo"}
> {mykey, ["bar", "baz"]}
>
> let's ignore the None case - in the case of the one or many values, i
> want to suck the values into a list. here's one way to do this:
>
> if mydict.has_key(mykey):
> vals=[]
> _v = mydict[mykey]
> if isinstance(_v, types.ListType):
> vals.extend(_v)
> else:
> vals.append(_v)
> # now we can safely iterate through acts
> for val in vals:
> .....
>
>
> my way is ugly. what's a better way?


If you have control over the dict's creation and 'filling', you may want
to handle the case there - eventually using a custom dict-like object
that stores everything (but None) in lists. The implementation code for
this object will not be less ugly than the one above, but at least
ugliness will be hidden away !-)

Else, you can shorten the code a bit (NB : ignoring the 'None' case...):

v = d[k]
v = ([v], v)[isinstance(v, list)]

And this can be hidden away in a function:

def get_as_list(d, k):
v = d[k]
return ([v], v)[isinstance(v, list)]

vals = get_as_list(mydict, mykey)


I don't like using isinstance() tests too much, but it depends on the
context...

HTH

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      07-31-2006
Bruno Desthuilliers <(E-Mail Removed)> writes:
> Too much useless lookup IMHO...


Actually, you, me, and Amit all mis-read David's original exapmle.
What he really wanted was (let's see if I get it right this time):

if mykey in mydict:
v = mydict[mykey]
if not isinstance(v, list):
v = [v]
for val in v: ...
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-31-2006
Amit Khemka wrote:
> On 7/31/06, David Zaret <(E-Mail Removed)> wrote:
>> i have a dict with a particular key - the values for this key will be
>> None, one valid scalar, or a list:
>>
>> {mykey, None}
>> {mykey, "foo"}
>> {mykey, ["bar", "baz"]}
>>
>> let's ignore the None case - in the case of the one or many values, i
>> want to suck the values into a list. here's one way to do this:
>>
>> if mydict.has_key(mykey):
>> vals=[]
>> _v = mydict[mykey]
>> if isinstance(_v, types.ListType):
>> vals.extend(_v)
>> else:
>> vals.append(_v)
>> # now we can safely iterate through acts
>> for val in vals:
>> .....

>
> how about:
>
> vals = []
> for val in mydict.values():
> try: vals.extend(val)
> except: vals.append(val)


>>> l = []
>>> l.extend((1, 2))
>>> l

[1, 2]
>>> l.extend('ab')
>>> l

[1, 2, 'a', 'b']
>>>



--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
David Zaret
Guest
Posts: n/a
 
      07-31-2006
thanks for the many responses.

i have zero control over the dict. in practice, i'm using turbogears
which is "automatically" populating the result dict with zero-to-many
choices from a generated list of HTML checkboxes. the user can select
none, one, or many, and submit. cherrypy packs the **kwargs arg full of
the selection, as i described in my op.

using a try/exception case to switch between append and extend will not
work. the scalar, coming back as a string, can certainly be iterated
and i'll end up with a bunch of characters in my list.

i like what's posted below - that's more compact - and at least closer
to what i was looking for.

thanks again, great newsgroup.

---- dz




Bruno Desthuilliers wrote:
> David Zaret wrote:
>> i have a dict with a particular key - the values for this key will be
>> None, one valid scalar, or a list:
>>
>> {mykey, None}
>> {mykey, "foo"}
>> {mykey, ["bar", "baz"]}
>>
>> let's ignore the None case - in the case of the one or many values, i
>> want to suck the values into a list. here's one way to do this:
>>
>> if mydict.has_key(mykey):
>> vals=[]
>> _v = mydict[mykey]
>> if isinstance(_v, types.ListType):
>> vals.extend(_v)
>> else:
>> vals.append(_v)
>> # now we can safely iterate through acts
>> for val in vals:
>> .....
>>
>>
>> my way is ugly. what's a better way?

>
> If you have control over the dict's creation and 'filling', you may want
> to handle the case there - eventually using a custom dict-like object
> that stores everything (but None) in lists. The implementation code for
> this object will not be less ugly than the one above, but at least
> ugliness will be hidden away !-)
>
> Else, you can shorten the code a bit (NB : ignoring the 'None' case...):
>
> v = d[k]
> v = ([v], v)[isinstance(v, list)]
>
> And this can be hidden away in a function:
>
> def get_as_list(d, k):
> v = d[k]
> return ([v], v)[isinstance(v, list)]
>
> vals = get_as_list(mydict, mykey)
>
>
> I don't like using isinstance() tests too much, but it depends on the
> context...
>
> HTH
>

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-31-2006
Paul Rubin wrote:
> Bruno Desthuilliers <(E-Mail Removed)> writes:
>> Too much useless lookup IMHO...

>
> Actually, you, me, and Amit all mis-read David's original exapmle.


Actually, I plea not guilty - cf my answer to the OP !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-31-2006
David Zaret wrote:
> thanks for the many responses.
>
> i have zero control over the dict. in practice, i'm using turbogears
> which is "automatically" populating the result dict with zero-to-many
> choices from a generated list of HTML checkboxes. the user can select
> none, one, or many, and submit. cherrypy packs the **kwargs arg full of
> the selection, as i described in my op.


Are you sure you get mydict[mykey]==None if the user doesn't check any
of the checkboxes ? According to html specs, the form's dataset
shouldn't have the key at all in this case.

> using a try/exception case to switch between append and extend will not
> work.


Nope.

> the scalar, coming back as a string, can certainly be iterated
> and i'll end up with a bunch of characters in my list.


Yes.

> i like what's posted below


Thanks - and BTW, please, don't top-post...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Safely add a key to a dict only if it does not already exist? Steven D'Aprano Python 12 01-19-2013 01:04 PM
Learning Pyhton - Functional Programming - How intersect/differencetwo dict with dict/values? fast! macm Python 11 11-11-2010 10:04 AM
Re: dict.keys() and dict.values() are always the same order, is it? Cameron Simpson Python 6 04-21-2010 04:37 AM
dict.keys() and dict.values() are always the same order, is it? Menghan Zheng Python 1 04-20-2010 03:51 AM



Advertisments