Dict Unpacking in Python

137 points

by

@_ZeD_

|

July 8th, 2025 at 4:05pm

@xg15

July 12th, 2025 at 2:45pm

Lame: Submit a PEP, campaign for community support, write a patch, go back and forth with the maintainers, endure weeks and months of bikeshedding, then maybe, eventually have your feature included in the next Python release.

Game: Use the codec hack, immediately publish your feature for all Python versions, then write "please do not use" to be safe.

@kristjansson

July 12th, 2025 at 3:35am

While not nearly as fun as the OP, I’d note that this sort of unpacking is very pleasant in the newish PEP636 match case statements:

https://peps.python.org/pep-0636/#matching-builtin-classes

@notpushkin

July 12th, 2025 at 1:09pm

So I see asottile has gone from backporting released features [1] to backporting unreleased ones!

[1]: https://pypi.org/p/future-fstrings, mentioned in https://github.com/asottile/dict-unpacking-at-home#please-do...

@zdimension

July 11th, 2025 at 11:12pm

Did not know that such things could be accomplished by registering a new file coding format. Reminds me of https://pypi.org/project/goto-statement/

@yde_java

July 12th, 2025 at 11:59am

I use the Python package 'sorcery' [0] in all my production services.

It gives dict unpacking but also a shorthand dict creation like this:

    from sorcery import dict_of, unpack_keys
    a, b = unpack_keys({'a': 1, 'b': 42})
    assert a == 1
    assert b == 42
    assert dict_of(a, b) == {'a': 1, 'b': 42}
[0] https://github.com/alexmojaki/sorcery

@zelphirkalt

July 11th, 2025 at 11:57pm

I found dictionary unpacking to be quite useful, when you don't want to mutate things. Code like:

    new_dict = {**old_dict, **update_keys_and_values_dict}
Or even complexer:

    new_dict = {
        **old_dict,
        **{
            key: val
            for key, val in update_keys_and_values_dict
            if key not in some_other_dict
        }
    }
It is quite flexible.

@frollogaston

July 12th, 2025 at 6:36pm

After using JS, Python dicts and objects feel so cumbersome. I don't see why they need to be separate things, and why you can't access a dict like `dict.key`. Destructuring is the icing on the cake. In JS, it even handles the named args use case like

   const foo = ({name, age, email}) => { }
I'm guessing all of this has been proposed in Python before, and rejected in part because at this point it'd create way more confusion than it's worth.

@sco1

July 12th, 2025 at 12:27am

The author also has an accompanying video: https://youtu.be/eqiM0xRmFJg

@nine_k

July 11th, 2025 at 11:50pm

In short, it runs a text preprocessor as the source text decoder (like you would decode from Latin-1 or Shift-JIS to Unicode).

@agumonkey

July 12th, 2025 at 4:38am

Coming from lisp/haskell I always wanted destructuring but after using it quite a lot in ES6/Typescript, I found it's not always as ergonomic and readable as I thought.

@qwertox

July 12th, 2025 at 6:31am

This confuses me a bit

  dct = {'a': [1, 2, 3]}
  {'a': [1, *rest]} = dct
  print(rest)  # [2, 3]
Does this mean that i can use?

  dct = {'a': [1, 2, 3]}
  {'b': [4, *rest]} = dct
  print(rest)  # [2, 3]
and more explicit

  dct = {'a': [1, 2, 3]}
  {'_': [_, *rest]} = dct
  print(rest)  # [2, 3]

@sametmax

July 12th, 2025 at 3:14pm

Anthony is also the maintainer of the deadsnake ppa, if you were searching for reasons to love him more.

@ziofill

July 12th, 2025 at 5:04pm

The confusing bit to me is that the LHS of this

{greeting, thing} = dct

is a set, which is not ordered, so why would greeting and thing be assigned in the order in which they appear?

@nikisweeting

July 12th, 2025 at 4:08am

I would donate $500 to the PSF tomorrow if they added this, the lack of it is daily pain

@andy99

July 12th, 2025 at 12:29am

  def u(**kwargs):
    return tuple(kwargs.values())
Am I missing something, is this effectively the same?

*I realize the tuple can be omitted here

@dogukancey

July 12th, 2025 at 6:37pm

[dead]

@unit149

July 12th, 2025 at 4:41am

[dead]

@odyssey7

July 12th, 2025 at 9:15am

Python needs a better dictionary. Also, Python needs better names for things than dict.