{"id":6120,"date":"2025-03-04T17:01:35","date_gmt":"2025-03-05T01:01:35","guid":{"rendered":"https:\/\/wonghoi.humgar.com\/blog\/?p=6120"},"modified":"2025-12-01T02:10:45","modified_gmt":"2025-12-01T10:10:45","slug":"how-missing-keys-are-handled-in-dictionary-hashtables-in-c-python-and-matlab","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2025\/03\/04\/how-missing-keys-are-handled-in-dictionary-hashtables-in-c-python-and-matlab\/","title":{"rendered":"How missing keys are handled in Dictionary (Hashtables) in C++, Python and MATLAB"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">C++<\/h2>\n\n\n\n<p>In C++ (STL), the default behavior to touching (especially reading) a missing key (whether it&#8217;s map or unordered_map, aka hashtable) is that <a href=\"https:\/\/www.youtube.com\/watch?v=lkgszkPnV8g&amp;t=437s\">the new key will be automatically inserted with the default value<\/a> for the type (say 0 for integers). <\/p>\n\n\n\n<p>I have an example <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2017\/11\/18\/c-annoyances-and-reliefs-operator-in-stl-map-based-containers\/\">MapWithDefault<\/a> template wrapper implementation that allows you to endow the map (during construction) with a default value to be returned when the key you&#8217;re trying to read does not exist<\/p>\n\n\n\n<p>C++ has the <code>at()<\/code> method but it involves throwing an exception when the key is not found. However enabling exceptions is a material performance overhead burden in C++.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MATLAB<\/h2>\n\n\n\n<p>MATLAB&#8217;s hashtables are done with <code>containers.Map()<\/code> and with more modern MATLAB, <code>dictionary<\/code> objects (R2020b and on), unless you want to stick to valid MATLAB variable names as keys and exploit dynamic fieldnames.<\/p>\n\n\n\n<p>Neither <code>containers.Map()<\/code> or <code>dictionary<\/code> have a default value mechanism when a key is not found. It will just throw an error if the key you are trying to read does not exist. Use <code>iskey()<\/code>\/<code>isKey()<\/code> method to check if the key exist first and decide to read it or spit out a default value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python<\/h2>\n\n\n\n<p>Natively Python dictionaries will throw a Key error exception if the requested key in <code>[]<\/code> operator (aka <code>__getitem__()<\/code>) does not already exist.<\/p>\n\n\n\n<p>Use <code>.get(key, default)<\/code> method if you want to assume a default value if the key is not found. The <code>.get()<\/code> method does not throw an exception: the <code>default<\/code> is <code>None<\/code> if not specified.<\/p>\n\n\n\n<p>If you want C++&#8217;s default behavior of reading a new key means inserting the said key with a default, you have to explicitly import <code>collections<\/code> package and use <code>defaultdict<\/code>. I wouldn&#8217;t recommend this as the behavior is not intuitive and likely confusing in insidious ways.<\/p>\n\n\n\n<p>There&#8217;s a simiar approach to my MapWithDefault in Python dictionaries: <a href=\"https:\/\/blog.finxter.com\/python-__missing__-magic-method\/\">subclass from <code>dict<\/code> and define your own <code>__missing__<\/code> dunder\/magic method<\/a> that returns a default when a key is missing, then use the parent (aka <code>dict<\/code>)&#8217;s constructor to do an explicit (type\/class) conversion for existing <code>dict<\/code> object into your child class object that has <code>__missing__<\/code> implemented.<\/p>\n\n\n\n<p>Despite this approach is a little like my MapWithDefault, the <code>__missing__<\/code> approach has more flexibility like allowing the default value to be dependent on the query key string, but it comes at the expense of making up a different <strong>class<\/strong> (namely a subclass of dict), not making a different <strong>instance<\/strong> per different default values.<\/p>\n\n\n\n<p>Monkey-patching instance methods is frowned upon in Python. So if you want the default value to tie to <strong>instances<\/strong>, the mechanism needs to be redesigned.<\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_6120\" class=\"pvc_stats all  \" data-element-id=\"6120\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>C++ In C++ (STL), the default behavior to touching (especially reading) a missing key (whether it&#8217;s map or unordered_map, aka hashtable) is that the new key will be automatically inserted with the default value for the type (say 0 for &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2025\/03\/04\/how-missing-keys-are-handled-in-dictionary-hashtables-in-c-python-and-matlab\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_6120\" class=\"pvc_stats all  \" data-element-id=\"6120\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[25,10,34],"tags":[],"class_list":["post-6120","post","type-post","status-publish","format-standard","hentry","category-cpp","category-matlab","category-python"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/6120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=6120"}],"version-history":[{"count":15,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/6120\/revisions"}],"predecessor-version":[{"id":6709,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/6120\/revisions\/6709"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=6120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=6120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=6120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}