Thanks for the elaboration;
in retrospect, given the simple requirement, that there are only two
dict keys, one of which is know and the other to be determined, maybe
just the direct dict methods are appropriate, e.g.
d = {'a': 'bob', 'b': 'stu'}
d_copy = dict(d)
d_copy.pop("a")
'bob'
d_copy.popit
Building on the answers of the others, a simple one liner, no side
effect, not the fastest I guess:
>>> d={'a': 'bob', 'b': 'stu'}
>>> set( d.keys() ).difference( [ 'a' ] ).pop()
'b'
Note the square brackets for the parameter of difference(). 'The
string 'a' and the list [ 'a' ] are both iterable
Vlastimil Brom wrote:
other_key = (set(data_dict.iterkeys()) - set([not_wanted_key,])).pop()
other_key = set(data_dict.iterkeys()).difference([not_wanted]).pop()
saves you the construction of an unnecessary set instance. At the
cost of a bit more verbosity, you can get rid of a second set:
2009/9/18 Ross :
>
> Learning my way around list comprehension a bit. I wonder if someone has a
> better way to solve this issue. I have a two element dictionary, and I know
> one of the keys but not the other, and I want to look up the other one.
>
> So I have this dictionary:
>
> aDict = {'a':
Thanks Tim (and Ishwor) for the suggestions, those are structures
that somewhat new to me - looks good! I'll play with those.At
this rate I may soon almost know what I'm doing.
Rgds
Ross.
On 18-Sep-09, at 1:19 PM, Tim Chase wrote:
Learning my way around list comprehension a bit. I w
Ross
Hi.
> So I have this dictionary:
>
> aDict = {'a': 'bob', 'b': 'stu'}
Yes.
> I know that the dictionary contains two keys/value pairs, but I don't know
> the values nor that the keys will be 'a' and 'b'. I finally get one of the
> keys passed to me as variable BigOne. e.g.:
>
> BigOne =
Learning my way around list comprehension a bit. I wonder if
someone has a better way to solve this issue. I have a two element
dictionary, and I know one of the keys but not the other, and I want
to look up the other one.
Several ways occur to me. Of the various solutions I played
wit
Learning my way around list comprehension a bit. I wonder if
someone has a better way to solve this issue. I have a two element
dictionary, and I know one of the keys but not the other, and I want
to look up the other one.
So I have this dictionary:
aDict = {'a': 'bob', 'b': 'stu'}
I