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 of one item too. If y
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 assume that what is p
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 r
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 prefer
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:
>
>>>>
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 sole(d):
... for o in d.value
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
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.
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 Sulzb
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 exception must be
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 b
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 unless it is clearly
int
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
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
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 a
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
>
> and that'd be an end
> 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
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 = {"
18 matches
Mail list logo