retrieve key of only element in a dictionary (Python 3)

2016-03-19 Thread Fillmore
I must be missing something simple, but... Python 3.4.0 (default, Apr 11 2014, 13:05:11) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> d = dict() >>> d['squib'] = "007" >>> # I forget that 'squib' is my key to retrieve the only element in d ...

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-19 Thread Tim Chase
On 2016-03-18 17:33, Fillmore wrote: > >>> d = dict() > >>> d['squib'] = "007" > >>> key = d.items()[0] I posted a similar question about 1-element-sets[1] a while back and Peter Otten & Rene Pijlman both suggested >>> s = set(["hello"]) >>> element, = s which, in your case would translate

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-19 Thread Martin A. Brown
OK, so ... I'll bite! >>> d = {"squib": "007"} >>> key, = d Why exactly does this work? I understand why the following three are similar and why they all work alike in this situation: key, = d (key,) = d [key] = d I also, intuitively understand that, if the dictionary d contains

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-19 Thread Gene Heskett
On Saturday 19 March 2016 04:42:29 Chris Angelico wrote: > On Sat, Mar 19, 2016 at 7:29 PM, Steven D'Aprano wrote: > > On Sat, 19 Mar 2016 12:36 pm, Chris Angelico wrote: > >> So unpacking will give you those keys - in an arbitrary order. Of > >> course, you don't care

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-19 Thread Daniel Wilcox
I think you're looking for something like popitem(). >>> d = {'asdf':1} >>> d.popitem()[0] 'asdf' Cheers On Fri, Mar 18, 2016 at 2:33 PM, Fillmore wrote: > > I must be missing something simple, but... > > Python 3.4.0 (default, Apr 11 2014, 13:05:11) > [GCC

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-19 Thread Chris Angelico
On Sat, Mar 19, 2016 at 7:29 PM, Steven D'Aprano wrote: > On Sat, 19 Mar 2016 12:36 pm, Chris Angelico wrote: > >> So unpacking will give you those keys - in an arbitrary order. Of >> course, you don't care about the order when there's only one. > > But what if you want the

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-19 Thread Steven D'Aprano
On Sat, 19 Mar 2016 12:36 pm, Chris Angelico wrote: > So unpacking will give you those keys - in an arbitrary order. Of > course, you don't care about the order when there's only one. But what if you want the key in reverse order? # Standard order [key] = mydict # Reverse order [yɘʞ] = mydict

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-19 Thread Larry Martell
On Fri, Mar 18, 2016 at 5:33 PM, Fillmore wrote: > > I must be missing something simple, but... > > Python 3.4.0 (default, Apr 11 2014, 13:05:11) > [GCC 4.8.2] on linux > Type "help", "copyright", "credits" or "license" for more information. d = dict()

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-18 Thread Chris Angelico
On Sat, Mar 19, 2016 at 8:33 AM, Fillmore wrote: > what am I missing? I don't want to iterate over the dictionary. > I know that there's only one element and I need to retrieve the key You could try: >>> next(iter(d)) Is that clean enough for usage? ChrisA --

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-18 Thread Terry Reedy
On 3/18/2016 5:39 PM, Daniel Wilcox wrote: I think you're looking for something like popitem(). d = {'asdf':1} d.popitem()[0] 'asdf' Only if he wants the item removed. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-18 Thread Fillmore
OK, this seems to do the trick, but boy is it a lot of code. Anythong more pythonic? >>> l = list(d.items()) >>> l [('squib', '007')] >>> l[0] ('squib', '007') >>> l[0][0] 'squib' >>> On 03/18/2016 05:33 PM, Fillmore wrote: I must be missing something simple, but... Python 3.4.0 (default,

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-18 Thread Martin A. Brown
>> But, I still don't understand why this works and can't puzzle it >> out. I see a sequence on the left of the assignment operator and a >> dictionary (mapping) on the right. > >When you iterate over a dictionary, you get its keys: > >scores = {"Fred": 10, "Joe": 5, "Sam": 8} >for person in

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-18 Thread Joel Goldstick
On Fri, Mar 18, 2016 at 6:08 PM, Fillmore wrote: > > OK, this seems to do the trick, but boy is it a lot of code. Anythong more > pythonic? > > >>> l = list(d.items()) > >>> l > [('squib', '007')] > >>> l[0] > ('squib', '007') > >>> l[0][0] > 'squib' Maybe this: l

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-18 Thread Chris Angelico
On Sat, Mar 19, 2016 at 12:27 PM, Martin A. Brown wrote: > But, I still don't understand why this works and can't puzzle it > out. I see a sequence on the left of the assignment operator and a > dictionary (mapping) on the right. When you iterate over a dictionary, you get

Re: retrieve key of only element in a dictionary (Python 3)

2016-03-18 Thread Chris Angelico
On Sat, Mar 19, 2016 at 10:03 AM, Tim Chase wrote: > >>> d = {"squib": "007"} > >>> key, = d > >>> key > 'squib' > > I'd put a comment on the line to make it clear what's going on since > that comma is easy to miss, but based on Alex Martelli's testing[2], >