{"id":168,"date":"2010-01-19T14:33:00","date_gmt":"2010-01-19T14:33:00","guid":{"rendered":"https:\/\/www.root42.de\/blog\/?p=168"},"modified":"2010-01-19T14:33:00","modified_gmt":"2010-01-19T14:33:00","slug":"elegantly-making-a-dictionary-of-lists","status":"publish","type":"post","link":"https:\/\/www.root42.de\/blog\/?p=168","title":{"rendered":"Elegantly making a dictionary of lists"},"content":{"rendered":"<p>In Python I sometimes want to make a dictionary of lists. So usually I would write something like:<\/p>\n<pre><br \/>mydic = {}<br \/>for something in somethingelse:<br \/>    mydic[somekey].append(something)<br \/><\/pre>\n<p>This however will not work, if somekey did not exist so far, because it will raise an exception. But there is help! You can use a <a href=\"http:\/\/docs.python.org\/library\/collections.html#collections.defaultdict\">collection<\/a>:<\/p>\n<pre><br \/>import collections<br \/>mydic = collections.defaultdic(list)<br \/>for something in somethingelse:<br \/>    mydic[somekey].append(something)<br \/><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Python I sometimes want to make a dictionary of lists. So usually I would write something like: mydic = {}for something in somethingelse: mydic[somekey].append(something) This however will not work, if somekey did not exist so far, because it will raise an exception. But there is help! You can use a collection: import collectionsmydic = &hellip; <a href=\"https:\/\/www.root42.de\/blog\/?p=168\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Elegantly making a dictionary of lists&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/168"}],"collection":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=168"}],"version-history":[{"count":0,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}