{"id":1593,"date":"2020-03-04T17:43:03","date_gmt":"2020-03-04T16:43:03","guid":{"rendered":"http:\/\/www.netnea.com\/cms\/?p=1593"},"modified":"2020-03-04T17:43:03","modified_gmt":"2020-03-04T16:43:03","slug":"auto-vivification-in-python-3-x","status":"publish","type":"post","link":"https:\/\/www.netnea.com\/cms\/2020\/03\/04\/auto-vivification-in-python-3-x\/","title":{"rendered":"Auto-vivification in Python 3.x"},"content":{"rendered":"\n<p>Are you spoiled by Perl&#8217;s auto-vivification allowing to write such code and you want to do the same in Python ?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> yourdict&#91;yourkey]&#91;newkey1]&#91;newkey2] = 42;<\/code><\/pre>\n\n\n\n<p>An often used, somewhat ugly code pattern where you need to add a key in a dict when it&#8217;s not there yet:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yourdict = {}\nfor item in items:\n    ...\n    if yourkey not in yourdict:\n        yourdict&#91;yourkey] = ()\n   yourdict&#91;yourkey].append(item)<\/code><\/pre>\n\n\n\n<p>There is a better way since Python 3.3 (even if Perl&#8217;s type of deep-structure on-the-fly creation is not there) :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from collections import defaultdict\nyourdict = defaultdict(list)\nfor item in items:\n    ...\n   yourdict&#91;yourkey].append(item)<\/code><\/pre>\n\n\n\n<p>oh, and you are running Python 3.x everywhere, correct ? Python 2.x is obsolete \/ end-of-life since 1.1.2020. <a href=\"https:\/\/www.netnea.com\/cms\/contact\/\">Talk to us<\/a> if you still run Python 2.x code and need help to port it to Python 3.x.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you spoiled by Perl&#8217;s auto-vivification allowing to write such code and you want to do the same in Python ? An often used, somewhat ugly code pattern where you need to add a key in a dict when it&#8217;s not there yet: There is a better way since Python 3.3 (even if Perl&#8217;s type [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":{"0":"post-1593","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python","7":"czr-hentry"},"_links":{"self":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts\/1593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/comments?post=1593"}],"version-history":[{"count":4,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts\/1593\/revisions"}],"predecessor-version":[{"id":1597,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts\/1593\/revisions\/1597"}],"wp:attachment":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/media?parent=1593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/categories?post=1593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/tags?post=1593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}