{"id":815,"date":"2015-01-15T10:11:33","date_gmt":"2015-01-15T18:11:33","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=815"},"modified":"2015-01-15T10:11:33","modified_gmt":"2015-01-15T18:11:33","slug":"dictionaries-or-associative-arrays-in-python","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/dictionaries-or-associative-arrays-in-python\/","title":{"rendered":"Dictionaries or Associative Arrays in Python"},"content":{"rendered":"

I have a couple previous articles around a similar topic but since I have not added any python code here is a short how to for future reference. It may be called associative arrays in other languages but Python calls it dicts or dictionaries.<\/p>\n

Related links:
\nhttp:\/\/blog.ls-al.com\/creating-a-javascript-array-with-one-to-many-type-relationship\/
\nhttp:\/\/blog.ls-al.com\/multi-array-in-bash\/
\nhttp:\/\/blog.ls-al.com\/multidimensional-array-in-python\/<\/p>\n

Manually constructing the dictionary:<\/p>\n

\r\nmailLists = {}\r\nmailLists['dba'] = ['joe', 'jim', 'jeff', 'john']\r\nmailLists['sa'] = ['mike', 'matt' ]\r\n\r\n#print mailLists\r\n\r\n#for key in mailLists:\r\n#  print key\r\n\r\nprint mailLists['dba']\r\nprint mailLists['sa']\r\n\r\nfor k,v in mailLists.iteritems():\r\n  print k + ": " + str(v)\r\n<\/pre>\n

For me the real value in this is for example when you build the lists. Think looping through an input file with lines like this:
\njon : dba<\/p>\n

Now you add jon to the array with key 'dba'. If the key does not exist we simply add one. If the key exists a new item is added to it. <\/p>\n

\r\nmailLists = {}\r\n\r\nwith open('b.txt') as input_file:\r\n  for i, line in enumerate(input_file):\r\n    #print line,\r\n    v,k = line.strip().split(':')\r\n    k = k.strip()\r\n    if k not in mailLists.keys():\r\n      mailLists[k] = []\r\n    mailLists[k].append(v)\r\n\r\nfor k,v in mailLists.iteritems():\r\n  print k + ": " + str(v)\r\n<\/pre>\n
\r\n# python b.py\r\ndba: ['jon ', 'jeff ']\r\nsa: ['matt ']\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

I have a couple previous articles around a similar topic but since I have not added any python code here<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-815","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=815"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/815\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}