[Python-ideas] Generator unpacking

Sven R. Kunze srkunze at mail.de
Mon Feb 15 17:23:39 EST 2016


On 15.02.2016 23:11, Sven R. Kunze wrote:
> On 15.02.2016 19:00, Jonathan Goble wrote:
>> On Mon, Feb 15, 2016 at 12:53 PM, Sven R. Kunze <srkunze at mail.de> wrote:
>>> On 15.02.2016 18:39, Ethan Furman wrote:
>>>> I /think/ I've used that trick to confirm an iterable was empty; I 
>>>> /know/
>>>> I've used
>>>>
>>>> [some_var] = some_var
>>>
>>> Is that an unknown Python idiom?
>>>
>>> It looks short but still insane.
>> It's actually a useful way to quickly unpack nested lists and tuples.
>> Here's a quick example:
>>
>> [snip]
>
> I am aware of (nested) unpacking in general. :)
>
>
> The use-case of checking if a list is has only a single element and to 
> unpack it in a single statement has simply never occurred to me.

In fact, I have been asked several times by some older Python developers 
on what the Pythonic way of doing exactly this is. Like this:

Dev: Sven, do you know a better way to get THE item from single-item 
list rather than just [0]?
Sven: huh? *distracted* yeah *thinking* why not [0]? seems simple enough 
to get the intent *back to topic*


I think because of that thread, I won't forget anymore. ;-)


Best,
Sven


More information about the Python-ideas mailing list