{"id":39173,"date":"2025-11-07T06:00:15","date_gmt":"2025-11-07T14:00:15","guid":{"rendered":"https:\/\/cloudinary.com\/blog\/?p=39173"},"modified":"2025-11-07T06:00:16","modified_gmt":"2025-11-07T14:00:16","slug":"how-does-the-match-statement-work-in-python","status":"publish","type":"post","link":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/","title":{"rendered":"How does the match statement work in Python?"},"content":{"rendered":"\n<p>You have probably seen code snippets using Python\u2019s match and case and wondered when to use them compared to if or dictionaries of callables. In community threads, developers often ask how structural pattern matching works in real code, what patterns exist, and how to avoid common pitfalls.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Question:<\/h2>\n\n\n\n<p><em>Hi all,<\/em><br><em>I am learning Python and keep seeing structural pattern matching. How does the match statement work in Python? What patterns can I match on, how do guards work, and what are the common gotchas? Sample code appreciated.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Answer:<\/h2>\n\n\n\n<p>Python\u2019s match statement provides structural pattern matching, introduced in 3.10. It lets you concisely branch on the shape and content of data rather than only on values. It works especially well with nested dicts, sequences, and dataclasses.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic syntax and literal patterns<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">def handle_status(code):\n\u00a0 \u00a0 match code:\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">200<\/span> | <span class=\"hljs-number\">201<\/span>:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"OK\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">400<\/span>:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"Bad Request\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">404<\/span>:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"Not Found\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> _:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"Unknown\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Match statements are an alternative to the traditional if\/else loops, allowing code to execute when specific parameters are met. Each case is checked against the input (in this example, <code>code<\/code>), and the first one that matches will be executed<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>_ is the default case, and should always be at the end of the match statement.<\/li>\n\n\n\n<li>The pipe operator | is used as an \u201cor\u201d operation. Both <code>200<\/code> or <code>201<\/code> will pass, returning \u201cOK\u201d.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Sequence and mapping patterns<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">def classify_point(p):\n\u00a0 \u00a0 match p:\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> &#91;x, y]:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> f<span class=\"hljs-string\">\"2D point: {x},{y}\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> &#91;x, y, z]:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> f<span class=\"hljs-string\">\"3D point: {x},{y},{z}\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> _:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"Not a point\"<\/span>\n\ndef read_headers(headers):\n\u00a0 \u00a0 match headers:\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> {<span class=\"hljs-string\">\"content-type\"<\/span>: ctype, <span class=\"hljs-string\">\"content-length\"<\/span>: length}:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> f<span class=\"hljs-string\">\"{ctype}, {length} bytes\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> {<span class=\"hljs-string\">\"content-type\"<\/span>: ctype}:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> f<span class=\"hljs-string\">\"{ctype}, length unknown\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> _:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"No content-type\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Class patterns and dataclasses<\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">from dataclasses import dataclass\n\n@dataclass\nclass Event:\n\u00a0 \u00a0 type: str\n\u00a0 \u00a0 payload: dict\n\ndef handle_event(evt):\n\u00a0 \u00a0 match evt:\n\u00a0 \u00a0 \u00a0 \u00a0 case Event(type=\"upload\", payload={\"format\": fmt, \"size\": s}):\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return f\"Upload of {fmt} with size {s}\"\n\u00a0 \u00a0 \u00a0 \u00a0 case Event(type=\"delete\", payload={\"id\": asset_id}):\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return f\"Delete {asset_id}\"\n\u00a0 \u00a0 \u00a0 \u00a0 case _:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return \"Unhandled event\"<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Guards for extra conditions<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">def parity(n):\n\u00a0 \u00a0 match n:\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> int() <span class=\"hljs-keyword\">if<\/span> n % <span class=\"hljs-number\">2<\/span> == <span class=\"hljs-number\">0<\/span>:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"even\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> int():\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"odd\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> _:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"not an int\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Guards are boolean expressions after a pattern using if. If the pattern matches but the guard fails, matching continues to the next case.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common pitfalls<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Name capture vs constants:<\/strong> Bare names capture values. To match a constant, qualify it or use literals.<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">from enum import Enum\nclass Color(Enum):\n\u00a0 \u00a0 RED = \"red\"\n\u00a0 \u00a0 BLUE = \"blue\"\n\ndef describe(c):\n\u00a0 \u00a0 match c:\n\u00a0 \u00a0 \u00a0 \u00a0 case Color.RED:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return \"red\"\n\u00a0 \u00a0 \u00a0 \u00a0 case Color.BLUE:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return \"blue\"\n\u00a0 \u00a0 \u00a0 \u00a0 case _:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return \"other\"<\/code><\/span><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Order matters:<\/strong> Put specific patterns before general ones to avoid unreachable cases.<\/li>\n\n\n\n<li><strong>Pattern shape must match:<\/strong> [x, y] will not match a tuple of three items or a dict.<\/li>\n\n\n\n<li><strong>Version requirement:<\/strong> Match statements are only supported in Python 3.10 and later.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Where match shines<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Parsing loosely structured payloads such as webhooks and API responses.<\/li>\n\n\n\n<li>Routing based on file metadata or media formats.<\/li>\n\n\n\n<li>Replacing long chains of nested if statements.<\/li>\n<\/ul>\n\n\n\n<p>If you routinely process images in Python scripts, this pairs nicely with media workflows. For example, you can decide how to save, convert, or optimize based on content-type or file extension.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using match to route media tasks, then delivering via Cloudinary<\/h3>\n\n\n\n<p>After you have a working pattern matcher, you can integrate it with delivery. For example, generate different transformation URLs based on the detected format and use Cloudinary to serve the result efficiently. If you are building URLs, see the concept of an <a href=\"https:\/\/cloudinary.com\/glossary\/image-url\">image URL<\/a>, or explore developer-friendly utilities on <a href=\"https:\/\/cloudinary.com\/tools\">Cloudinary Tools<\/a>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-comment\"># Example: route media handling with match, then build a delivery URL<\/span>\ndef build_delivery(public_id, info):\n\u00a0 \u00a0 match info:\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> {<span class=\"hljs-string\">\"format\"<\/span>: <span class=\"hljs-string\">\"webp\"<\/span>, <span class=\"hljs-string\">\"width\"<\/span>: w} <span class=\"hljs-keyword\">if<\/span> w &gt; <span class=\"hljs-number\">1200<\/span>:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># Convert to JPEG and resize for broad compatibility<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> f<span class=\"hljs-string\">\"https:\/\/res.cloudinary.com\/demo\/image\/upload\/c_fill,w_1200,f_jpg\/{public_id}.jpg\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> {<span class=\"hljs-string\">\"format\"<\/span>: <span class=\"hljs-string\">\"heic\"<\/span>}:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># Convert HEIC to JPEG on delivery<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> f<span class=\"hljs-string\">\"https:\/\/res.cloudinary.com\/demo\/image\/upload\/f_jpg\/{public_id}.jpg\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> {<span class=\"hljs-string\">\"format\"<\/span>: <span class=\"hljs-string\">\"png\"<\/span>}:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># Optimize PNG with smart resizing<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> f<span class=\"hljs-string\">\"https:\/\/res.cloudinary.com\/demo\/image\/upload\/c_scale,w_800\/{public_id}.png\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">case<\/span> _:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># Default passthrough<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> f<span class=\"hljs-string\">\"https:\/\/res.cloudinary.com\/demo\/image\/upload\/{public_id}\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This approach keeps your Python logic clean while delegating heavy lifting to a CDN-backed transformation layer. You can expand the pattern to include thumbnails, watermarks, or responsive breakpoints by adding more cases.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">TL;DR<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Match statements provide structural pattern matching for values, sequences, mappings, and classes.<\/li>\n\n\n\n<li>Use guards for additional conditions and put specific patterns before general ones.<\/li>\n\n\n\n<li>Avoid bare names for constants, prefer qualified names or literals.<\/li>\n\n\n\n<li>Great for routing and parsing tasks. Combine with URL based delivery to streamline media pipelines.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Learn More<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/cloudinary.com\/tools\/jpg-to-webp\">JPG to WebP Converter<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/tools\/png-to-webp\">PNG to WebP Converter<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/tools\/image-upscale\">Image Upscale<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/background-remover\">Background Remover<\/a><\/li>\n<\/ul>\n\n\n\n<p>Ready to build smarter media flows with clean Python branching and fast delivery? <a href=\"https:\/\/cloudinary.com\/users\/register_free\">Create your free Cloudinary account<\/a> and start optimizing today.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You have probably seen code snippets using Python\u2019s match and case and wondered when to use them compared to if or dictionaries of callables. In community threads, developers often ask how structural pattern matching works in real code, what patterns exist, and how to avoid common pitfalls. Question: Hi all,I am learning Python and keep [&hellip;]<\/p>\n","protected":false},"author":88,"featured_media":39174,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_cloudinary_featured_overwrite":false,"footnotes":""},"categories":[1],"tags":[423],"class_list":["post-39173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-questions"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How does the match statement work in Python?<\/title>\n<meta name=\"description\" content=\"You have probably seen code snippets using Python\u2019s match and case and wondered when to use them compared to if or dictionaries of callables. In community\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How does the match statement work in Python?\" \/>\n<meta property=\"og:description\" content=\"You have probably seen code snippets using Python\u2019s match and case and wondered when to use them compared to if or dictionaries of callables. In community\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudinary Blog\" \/>\n<meta property=\"article:published_time\" content=\"2025-11-07T14:00:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-07T14:00:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762523975\/how_does_the_match_statement_in_python_work_featured_image\/how_does_the_match_statement_in_python_work_featured_image.jpg?_i=AA\" \/>\n\t<meta property=\"og:image:width\" content=\"2000\" \/>\n\t<meta property=\"og:image:height\" content=\"1100\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"damjanantevski\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"NewsArticle\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/\"},\"author\":{\"name\":\"damjanantevski\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e\"},\"headline\":\"How does the match statement work in Python?\",\"datePublished\":\"2025-11-07T14:00:15+00:00\",\"dateModified\":\"2025-11-07T14:00:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/\"},\"wordCount\":541,\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762523975\/how_does_the_match_statement_in_python_work_featured_image\/how_does_the_match_statement_in_python_work_featured_image.jpg?_i=AA\",\"keywords\":[\"Questions\"],\"inLanguage\":\"en-US\",\"copyrightYear\":\"2025\",\"copyrightHolder\":{\"@id\":\"https:\/\/cloudinary.com\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/\",\"url\":\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/\",\"name\":\"How does the match statement work in Python?\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762523975\/how_does_the_match_statement_in_python_work_featured_image\/how_does_the_match_statement_in_python_work_featured_image.jpg?_i=AA\",\"datePublished\":\"2025-11-07T14:00:15+00:00\",\"dateModified\":\"2025-11-07T14:00:16+00:00\",\"description\":\"You have probably seen code snippets using Python\u2019s match and case and wondered when to use them compared to if or dictionaries of callables. In community\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#primaryimage\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762523975\/how_does_the_match_statement_in_python_work_featured_image\/how_does_the_match_statement_in_python_work_featured_image.jpg?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762523975\/how_does_the_match_statement_in_python_work_featured_image\/how_does_the_match_statement_in_python_work_featured_image.jpg?_i=AA\",\"width\":2000,\"height\":1100},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudinary.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How does the match statement work in Python?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"name\":\"Cloudinary Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudinary.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\",\"name\":\"Cloudinary Blog\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"width\":312,\"height\":60,\"caption\":\"Cloudinary Blog\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e\",\"name\":\"damjanantevski\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g\",\"caption\":\"damjanantevski\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How does the match statement work in Python?","description":"You have probably seen code snippets using Python\u2019s match and case and wondered when to use them compared to if or dictionaries of callables. In community","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How does the match statement work in Python?","og_description":"You have probably seen code snippets using Python\u2019s match and case and wondered when to use them compared to if or dictionaries of callables. In community","og_url":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/","og_site_name":"Cloudinary Blog","article_published_time":"2025-11-07T14:00:15+00:00","article_modified_time":"2025-11-07T14:00:16+00:00","og_image":[{"width":2000,"height":1100,"url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762523975\/how_does_the_match_statement_in_python_work_featured_image\/how_does_the_match_statement_in_python_work_featured_image.jpg?_i=AA","type":"image\/jpeg"}],"author":"damjanantevski","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#article","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/"},"author":{"name":"damjanantevski","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e"},"headline":"How does the match statement work in Python?","datePublished":"2025-11-07T14:00:15+00:00","dateModified":"2025-11-07T14:00:16+00:00","mainEntityOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/"},"wordCount":541,"publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762523975\/how_does_the_match_statement_in_python_work_featured_image\/how_does_the_match_statement_in_python_work_featured_image.jpg?_i=AA","keywords":["Questions"],"inLanguage":"en-US","copyrightYear":"2025","copyrightHolder":{"@id":"https:\/\/cloudinary.com\/#organization"}},{"@type":"WebPage","@id":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/","url":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/","name":"How does the match statement work in Python?","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#primaryimage"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762523975\/how_does_the_match_statement_in_python_work_featured_image\/how_does_the_match_statement_in_python_work_featured_image.jpg?_i=AA","datePublished":"2025-11-07T14:00:15+00:00","dateModified":"2025-11-07T14:00:16+00:00","description":"You have probably seen code snippets using Python\u2019s match and case and wondered when to use them compared to if or dictionaries of callables. In community","breadcrumb":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#primaryimage","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762523975\/how_does_the_match_statement_in_python_work_featured_image\/how_does_the_match_statement_in_python_work_featured_image.jpg?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762523975\/how_does_the_match_statement_in_python_work_featured_image\/how_does_the_match_statement_in_python_work_featured_image.jpg?_i=AA","width":2000,"height":1100},{"@type":"BreadcrumbList","@id":"https:\/\/cloudinary.com\/blog\/questions\/how-does-the-match-statement-work-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudinary.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How does the match statement work in Python?"}]},{"@type":"WebSite","@id":"https:\/\/cloudinary.com\/blog\/#website","url":"https:\/\/cloudinary.com\/blog\/","name":"Cloudinary Blog","description":"","publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudinary.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cloudinary.com\/blog\/#organization","name":"Cloudinary Blog","url":"https:\/\/cloudinary.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","width":312,"height":60,"caption":"Cloudinary Blog"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e","name":"damjanantevski","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g","caption":"damjanantevski"}}]}},"jetpack_featured_media_url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762523975\/how_does_the_match_statement_in_python_work_featured_image\/how_does_the_match_statement_in_python_work_featured_image.jpg?_i=AA","_links":{"self":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/39173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/users\/88"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/comments?post=39173"}],"version-history":[{"count":1,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/39173\/revisions"}],"predecessor-version":[{"id":39175,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/39173\/revisions\/39175"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media\/39174"}],"wp:attachment":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media?parent=39173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/categories?post=39173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/tags?post=39173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}