{"id":159,"date":"2010-04-23T09:37:37","date_gmt":"2010-04-23T14:37:37","guid":{"rendered":"http:\/\/www.popmartian.com\/tipsntricks\/?p=159"},"modified":"2011-03-30T10:51:12","modified_gmt":"2011-03-30T15:51:12","slug":"python-howto-push-a-dict-on-to-a-list-push-a-hash-on-to-an-array","status":"publish","type":"post","link":"https:\/\/www.popmartian.com\/tipsntricks\/2010\/04\/23\/python-howto-push-a-dict-on-to-a-list-push-a-hash-on-to-an-array\/","title":{"rendered":"Python HOWTO Push a Dict on to a List (push a hash on to an array)"},"content":{"rendered":"<p>One of my more popular posts is <a href=\"http:\/\/www.popmartian.com\/tipsntricks\/2008\/08\/12\/perl-howto-push-a-hash-onto-an-array\/\">How to Push a Hash on to an Array in PERL<\/a>, but how do you push a dict on to a list in Python?<\/p>\n<p>Lists are one-dimensional arrays and dicts are associative arrays or hash arrays.\u00c2\u00a0 This means we can do the same thing we do in other languages, with syntax to match Python&#8217;s object-oriented data structures.<\/p>\n<p><code>import pprint<\/code><\/p>\n<p><code># Define the list<br \/>\nsomelist = []<\/code><\/p>\n<p><code># Do add some elements to the list<br \/>\nsomelist.append({'key1':'value1', 'key2': 'value2'})<br \/>\nsomelist.append({'key1':'value1', 'key2': 'value2'})<br \/>\nsomelist.append({'key1':'value1', 'key2': 'value2'})<\/code><\/p>\n<p><code># Print it out<br \/>\npp = pprint.PrettyPrinter(indent=4)<br \/>\npp.pprint(somelist)<\/code><\/p>\n<p>Will give you:<\/p>\n<p><code>[\u00c2\u00a0\u00c2\u00a0 {\u00c2\u00a0\u00c2\u00a0 'key1': 'value1', 'key2': 'value2'},<br \/>\n{\u00c2\u00a0\u00c2\u00a0 'key1': 'value1', 'key2': 'value2'},<br \/>\n{\u00c2\u00a0\u00c2\u00a0 'key1': 'value1', 'key2': 'value2'}]<\/code><\/p>\n<blockquote><p>Did you find this post useful or have questions or comments?  Please let  me know!<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>One of my more popular posts is How to Push a Hash on to an Array in PERL, but how do you push a dict on to a list in Python? Lists are one-dimensional arrays and dicts are associative arrays &hellip; <a href=\"https:\/\/www.popmartian.com\/tipsntricks\/2010\/04\/23\/python-howto-push-a-dict-on-to-a-list-push-a-hash-on-to-an-array\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,9,59,8],"tags":[],"class_list":["post-159","post","type-post","status-publish","format-standard","hentry","category-how-to-do-stuff","category-programming","category-python","category-software"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/comments?post=159"}],"version-history":[{"count":8,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/159\/revisions"}],"predecessor-version":[{"id":189,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/159\/revisions\/189"}],"wp:attachment":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/media?parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/categories?post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/tags?post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}