Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

22
  • 3
    Strings only limitation for keywords expansion is enough to rule out {**x, **y} method. However, the items approach can be made workable by converting dictitems to list like dict(list(x.items()), list(y.items())). Commented May 16, 2019 at 15:00
  • 33
    @MohammadAzim "strings only" only applies to keyword argument expansion in callables, not generalized unpacking syntax. To demonstrate that this works: {**{(0, 1):2}} -> {(0, 1): 2}
    – Aaron Hall
    Commented May 16, 2019 at 16:07
  • 20
    Hi, the top is a summary, yes. Up to you. The whole thing would be a great blog post. Note Py 3.4 and below are EOL, 3.5 approaching EOL in 2020-09. Commented Mar 13, 2020 at 2:09
  • 28
    I agree with the eagerness to leave the old way behind, but sometimes people have to work in environments where they only have the older technology available to them. People also have to update code, and seeing the old way next to the new way allows them to confidently replace the old code with equivalent new code. I am open to suggestions on reorganizing the material, but I think we need to keep the older information.
    – Aaron Hall
    Commented May 17, 2020 at 15:04
  • 4
    Wow! That's a thorough answer. One minor comment, though: "the intended usage for dict(**y) is for creating dictionaries for readability purposes". I would argue that it's also to make the code less error prone, because dict(a=17, b=19, a=23) would fail with "SyntaxError: keyword argument repeated", while {"a": 17, "b": 19, "a": 23} would not (with 23 overwriting 17) and your accidental double key "a" would've gone unnoticed. Of course, if you want to allow double keys (I cannot see why, though), then {...} syntax is the way to go. Many linters, of course, warn of double keys. Commented Sep 8, 2020 at 15:04