[Python-ideas] Unpacking a dict

Chris Angelico rosuav at gmail.com
Wed May 25 22:56:12 EDT 2016

On Thu, May 26, 2016 at 12:43 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> I'll meet you halfway:
>   {a, b} = some_dict
> which is currently a SyntaxError, so little backwards compatibility
> concerns, plus it clearly state the mapping should have two elements,
> similarly to
>  [a] = some_iterable
> and
>  (b) = some_iterable
> both clearly state that a one-element iterable is being unpacked.

Careful - the second one doesn't:

>>> (b) = [1, 2, 3]

Parens don't make a tuple, and that includes with unpacking. You'd be
correct if you had a comma in there, though.

I hope there doesn't end up being a confusion between mapping
unpacking and set display. Sets are a bit of an odd duck; are they
like lists only unordered, or like mappings only without values? I've
seen them used both ways, and the syntax is somewhere between the two.
Having a syntax that constructs a set if used on the RHS but unpacks a
dict if used on the LHS seems to violate syntactic purity, but I'd be
happy to let practicality trump that.


More information about the Python-ideas mailing list