
{"id":5292,"date":"2025-07-31T19:11:43","date_gmt":"2025-07-31T23:11:43","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=5292"},"modified":"2025-07-31T19:12:49","modified_gmt":"2025-07-31T23:12:49","slug":"redis-hset-with-items-parameter","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=5292","title":{"rendered":"Redis: hset with &#8216;items&#8217; parameter"},"content":{"rendered":"<p>In Python, Redis.hset function has &#8216;items&#8217; that, per documentation<br \/>\n<i>&#8220;accepts a list of key\/value pairs that will be added to hash &#8220;name&#8220;.&#8221;<\/i>.<\/p>\n<p>You would think that it&#8217;s something like <code>[(\"key1\", \"value1\"), (\"key2\"), (\"value2\")]<\/code>?<br \/>\nWrong!<\/p>\n<p>It&#8217;s <code>[\"key1\", \"value1\", \"key2\", \"value2\"]<\/code>.<\/p>\n<p>So much for &#8220;key-value pairs&#8221;. If you give it an odd number of elements, it barfs with <code>\"redis.exceptions.ResponseError: wrong number of arguments for 'hset' command\"<\/code>.<\/p>\n<p>The <code>items<\/code> parameter seems to be a foster child of the <code>hset<\/code> method. Google AI was unable to give me a usage example: I mean, it of course said &#8220;here is an example using the items parameter&#8221;, but the example itself did not contain the word &#8216;item&#8217;. ChatGPT claimed that &#8216;items&#8217; must be an old deprecated parameter name or a confusion with <code>dict.items()<\/code>. Anyway, here&#8217;s a working piece of code:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport redis\r\n\r\nr = redis.Redis(host=&quot;localhost&quot;, port=6379, db=0, decode_responses=True)\r\nr.hset(&quot;country:42&quot;, items=&#x5B;&quot;language&quot;, &quot;Flemish&quot;, &quot;capital&quot;, &quot;Brussels&quot;])\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Python, Redis.hset function has &#8216;items&#8217; that, per documentation &#8220;accepts a list of key\/value pairs that will be added to hash &#8220;name&#8220;.&#8221;. You would think that it&#8217;s something like [(&#8220;key1&#8221;, <a href=\"https:\/\/ikriv.com\/blog\/?p=5292\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4,26],"tags":[],"class_list":["entry","author-ikriv","post-5292","post","type-post","status-publish","format-standard","category-hack","category-python"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5292"}],"version-history":[{"count":3,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5292\/revisions"}],"predecessor-version":[{"id":5295,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5292\/revisions\/5295"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}