[Python-ideas] Unpacking a dict

Nathan Schneider neatnate at gmail.com
Thu May 26 18:28:25 EDT 2016


On Thu, May 26, 2016 at 8:48 PM, MRAB <python at mrabarnett.plus.com> wrote:

> On 2016-05-26 20:25, Paul Moore wrote:
> [snip]
>
> Personally, though, I don't see *that* much wrong with
>>
>>     partner_id = values['partner_id']
>>     product_id = values['product_id']
>>     ship_to = values['ship_to']
>>     product_ids = values['product_ids']
>>
>> It's a bit repetitive, and maybe a little verbose, but nothing a good
>> editor or IDE (or anything better than gmail's web interface :-))
>> wouldn't make straightforward to manage.
>>
>> Could we use semicolons in the subscript to create a tuple? They could be
> used for packing or unpacking:
>
>     partner_id, product_id, ship_to, product_ids = values['partner_id';
> 'product_id'; 'ship_to'; 'product_ids']
>
>     my_dict['partner_id'; 'product_id'; 'ship_to'; 'product_ids'] =
> partner_id, product_id, ship_to, product_ids
>
>
Instead of special syntax, what if dict.values() returned a tuple when
given keys as arguments:

   partner_id, product_id, ship_to, product_ids =
my_dict.values('partner_id', 'product_id', 'ship_to', 'product_ids')

That avoids repeating the dict variable, at least. And as there is
dict.update(), I don't see the need for a new syntax for assigning to
multiple keys.

Nathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160526/21e1fa9b/attachment.html>


More information about the Python-ideas mailing list