r/Python • u/Iron-Man-2008 • 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/
40
Upvotes
View all comments
Show parent comment
3
u/Iron-Man-2008 Aug 27 '26
case {"key": "value"}: ...could have matched the exact shape while allowingcase {"key": "value", **_}: ...to ignore extra keys without binding them to a variable the same waycase [1, 2, *_]ignores extra elements without binding them to a variable.