Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-13 Thread Bernardo Sulzbach
On Tue, Jan 12, 2016 at 11:29 PM, Steven D'Aprano wrote: > On Wed, 13 Jan 2016 06:12 am, Bernardo Sulzbach wrote: > >> I saw it in another answer. next(iter(d)) is still the winner. > > Except that doesn't return the *value*, it returns the *key*. > There is a typo, sorry. I

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-13 Thread Grant Edwards
On 2016-01-13, Steven D'Aprano wrote: > Probably the best solution, because it will conveniently raise an exception > if your assumption that the dict has exactly one item is wrong: > > item, = d.values() # Note the comma after "item". [...] > but you can unpack a sequence

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Peter Otten
Ian Kelly wrote: > On Tue, Jan 12, 2016 at 10:12 AM, Terry Reedy wrote: >> Using the values views at intended (as an iterable): >> > dv = d.values() > next(iter(dv)) >> 1 > > Good coding practice also dictates that whenever next is called, the > potential StopIteration

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Bernardo Sulzbach
On Tue, Jan 12, 2016 at 3:32 PM, Peter Otten <__pete...@web.de> wrote: > > If there is exactly one item you can unpack: > d = {"Wilf's Cafe": 1} k, = d.values() k > 1 > I personally don't like that trailing comma, it just looks wrong there. But this is very neat. -- Bernardo

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Marko Rauhamaa
Jussi Piitulainen : > But the most readable thing might be to have a function that extracts > the sole value by whatever means: > >>>> def sole(d): [o] = d.values() ; return o >... >>>> sole(shoe) >3.141592653589793 In the same vein: >>> def

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Jussi Piitulainen
Nick Mellor writes: > Hi all, > > Seemingly simple problem: > > There is a case in my code where I know a dictionary has only one item > in it. I want to get the value of that item, whatever the key is. > > In Python2 I'd write: > d = {"Wilf's Cafe": 1} d.values()[0] > 1 > > and that'd

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Jussi Piitulainen
Marko Rauhamaa writes: > Jussi Piitulainen: > >> But the most readable thing might be to have a function that extracts >> the sole value by whatever means: >> >>>>> def sole(d): [o] = d.values() ; return o >>... >>>>> sole(shoe) >>3.141592653589793 > > In the same vein: > >

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Peter Otten
Bernardo Sulzbach wrote: > On Tue, Jan 12, 2016 at 3:32 PM, Peter Otten <__pete...@web.de> wrote: >> >> If there is exactly one item you can unpack: >> > d = {"Wilf's Cafe": 1} > k, = d.values() > k >> 1 >> > > I personally don't like that trailing comma, it just looks wrong > there.

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Bernardo Sulzbach
I saw it in another answer. next(iter(d)) is still the winner. This resembles a list just too much, making the coder's intent harder to understand. This is **very** subjective, of course. -- https://mail.python.org/mailman/listinfo/python-list

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Steven D'Aprano
On Wed, 13 Jan 2016 03:50 am, Nick Mellor wrote: > Hi all, > > Seemingly simple problem: > > There is a case in my code where I know a dictionary has only one item in > it. I want to get the value of that item, whatever the key is. [snip examples] > None of this feels like the "one, and

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Steven D'Aprano
On Wed, 13 Jan 2016 06:12 am, Bernardo Sulzbach wrote: > I saw it in another answer. next(iter(d)) is still the winner. Except that doesn't return the *value*, it returns the *key*. py> d = {'key': 'value'} py> next(iter(d)) 'key' To get the value: py> next(iter(d.values())) 'value' > This

subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Nick Mellor
Hi all, Seemingly simple problem: There is a case in my code where I know a dictionary has only one item in it. I want to get the value of that item, whatever the key is. In Python2 I'd write: >>> d = {"Wilf's Cafe": 1} >>> d.values()[0] 1 and that'd be an end to it. In Python 3: >>> d =

RE: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Emanuel Barry
> Hi all, > > Seemingly simple problem: > > There is a case in my code where I know a dictionary has only one item in it. > I want to get the value of that item, whatever the key is. > > In Python2 I'd write: > > >>> d = {"Wilf's Cafe": 1} > >>> d.values()[0] > 1 The equivalent in Python 3 is

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Chris Angelico
On Wed, Jan 13, 2016 at 3:50 AM, Nick Mellor wrote: > There is a case in my code where I know a dictionary has only one item in it. > I want to get the value of that item, whatever the key is. > > In Python2 I'd write: > d = {"Wilf's Cafe": 1} d.values()[0] > 1

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Terry Reedy
On 1/12/2016 11:50 AM, Nick Mellor wrote: Hi all, Seemingly simple problem: There is a case in my code where I know a dictionary has only one item in it. I want to get the value of that item, whatever the key is. In Python2 I'd write: d = {"Wilf's Cafe": 1} d.values()[0] 1 and that'd be

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Bernardo Sulzbach
Intentions aside, next(iter(...)) seems the most pythonic you can get about this anyway. Just in case you happen not to need the dictionary anymore, d.popitem()[1] does the trick. -- https://mail.python.org/mailman/listinfo/python-list

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Peter Otten
Nick Mellor wrote: > Hi all, > > Seemingly simple problem: > > There is a case in my code where I know a dictionary has only one item in > it. I want to get the value of that item, whatever the key is. > > In Python2 I'd write: > d = {"Wilf's Cafe": 1} d.values()[0] > 1 > > and

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

2016-01-12 Thread Ian Kelly
On Tue, Jan 12, 2016 at 10:12 AM, Terry Reedy wrote: > Using the values views at intended (as an iterable): > dv = d.values() next(iter(dv)) > 1 Good coding practice also dictates that whenever next is called, the potential StopIteration exception must be caught