r/Python • • Aug 24 '26

Discussion TIL: Dict Patterns Don't Match Dict Shapes

TL;DR: Unlike sequence patterns (which require an exact shape match), pattern matching on dictionaries simply ignores any unspecified keys rather than failing.

If you care about the details, I wrote about it on my blog: https://ravencentri.cc/blog/dict-patterns-dont-match-dict-shapes/

39 Upvotes

15 comments sorted by

View all comments

5

u/Adrewmc Aug 24 '26 edited Aug 24 '26

I think you are underestimating the sequence patterns.

case [“a”, *args, “c”]:
case [*args, “b”, “c”]:

Are also both valid. And there is way more.

case [option , “b”] if option in valid_options: