{"id":1854,"date":"2021-12-04T10:00:51","date_gmt":"2021-12-04T16:00:51","guid":{"rendered":"https:\/\/blog.iqonda.net\/?p=1854"},"modified":"2021-12-04T10:00:51","modified_gmt":"2021-12-04T16:00:51","slug":"python-append-key","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/python-append-key\/","title":{"rendered":"python append key"},"content":{"rendered":"

Python Append Key<\/h1>\n

Building a dict and ordering it into groups by key is sometimes very useful.<\/p>\n

Teh following code show using the if .. in option and defaultdict option of checkign for a key when loading the dictionary. Although people warn that using has_key or if .. in type checks slows it down a lot my timings was fairly similar. I left some commented code in for my own reference.<\/p>\n

source<\/h2>\n
from collections import defaultdict\nfrom time import time\n\nsample_size = 10000000\n\ndct1 = defaultdict(list)\ndct1 = {}\nst_time = time()\n\nfor i in range(1, sample_size):\n    s = str(i)\n    key = s[0:1]\n    name = 'server' + s\n    dct1.setdefault(key, []).append({'name':name,'status':'RUNNING'})  # returns None!\n\nprint (f\"\\ndct1 defaultdict option: {time() - st_time}\")\n\n#print (dct1)  \n# get one key\n#one_key = dct1.get('2')\n#print (one_key)\n#for v in one_key:\n#  print (v)\n\n# print by key\n#for k,v in dct1.items():\n#  print(\"\\nkey == {}\".format(k))\n#  print (v)\n#  #for i in v:\n#  #  print(\"  {} {}\".format(i[\"name\"], i[\"status\"]))\n\ndct2 = {}\nst_time = time()\n\nfor i in range(1, sample_size):\n    s = str(i)\n    key = s[0:1]\n    name = 'server' + s\n    if key in dct2:\n      dct2[key].append({'name':name,'status': 'STOPPED'})\n    else:\n      dct2.update({key: [{'name': name,'status': 'STOPPED'}]})\n\nprint (f\"\\ndct2 if .. in option: {time() - st_time}\")\n\n#print (dct2)\n#one_key = dct1.get('1')\n#print (one_key)\n#for v in one_key:\n#  print (v)\n\n# print by key\n#for k,v in dct2.items():\n#  print(\"\\nkey == {}\".format(k))\n#  print (v)\n#  #for i in v:\n#  #  print(\"  {} {}\".format(i[\"name\"], i[\"status\"]))<\/code><\/pre>\n

test<\/h2>\n
py-assoc-arr$ python3 py-keyed-dict-timing.py \n\ndct1 defaultdict option: 6.392352342605591\n\ndct2 if .. in option: 6.472132921218872\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

Python Append Key Building a dict and ordering it into groups by key is sometimes very useful. Teh following code<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-1854","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1854","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=1854"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1854\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}