[Python-ideas] Unpacking a dict

Michel Desmoulin desmoulinmichel at gmail.com
Thu May 26 10:10:10 EDT 2016


We could provide several options though.

a, b, c = **mapping
a, b, **c = **mapping

For the simple case.

AND

{"1": a, "foo.bar": b, **c} = **mapping

For more complex cases.

We already have suitabilities with regular unpacking such as:

a, (b, c) = 1, range(2)

The thing with those details is that you can completly ignore them, and
don't know they exist, and simply look it up when you need it.

But I must say:

{"1": a, "foo.bar": b, **c} = **mapping

Looks very ugly.

Le 26/05/2016 07:48, Terry Reedy a écrit :
> On 5/26/2016 1:09 AM, Michael Selik wrote:
>> I agree that comprehensions plus tuple unpacking handle many possible
>> use cases for dict unpacking.
> 
> There is another dict method that has been ignored in this discussion.
> 
>>>> mapping = {'a': 1, 'b': 2, 'c': 3}
>>>> x = mapping.pop('a')
>>>> mapping
> {'c': 3, 'b': 2}
> 
> We have both subscripting and popping to handle getting a value and
> either leaving or removing the pair.
> 


More information about the Python-ideas mailing list