{"id":1185,"date":"2022-08-30T15:13:48","date_gmt":"2022-08-30T15:13:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-check-if-list-variable-exists-in-python-mako-template-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:13:48","modified_gmt":"2022-08-30T15:13:48","slug":"how-to-check-if-list-variable-exists-in-python-mako-template-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-check-if-list-variable-exists-in-python-mako-template-collection-of-common-programming-errors\/","title":{"rendered":"How to check if list variable exists in python mako template?-Collection of common programming errors"},"content":{"rendered":"<p>Assuming I have the following in my template:<\/p>\n<pre><code>% if not mydict['somekey'] is UNDEFINED:\n    ${mydict['somekey'][0]['hellothere']}\n% endif    \n<\/code><\/pre>\n<p>My issue is the above does not work as <code>mydict['somekey']<\/code> is always an array, but it could be empty. I want to be able to check to make sure that if <code>mydict['somekey']<\/code> is defined, I can add a check to make sure either 1) the list size is greater than 0 (from inside the template) or if the <code>mydict['somekey']<\/code> has elements in it so that I can print out what is in <code>mydict['somekey'][0]['hellothere']<\/code> when available.<\/p>\n<p>What must I do? I keep getting an:<\/p>\n<pre><code>IndexError: list index out of range\n<\/code><\/pre>\n<p>with the above<\/p>\n<ol>\n<li>\n<p>PEP 8 recommends:<\/p>\n<blockquote>\n<p>For sequences, (strings, lists, tuples), use the fact that empty sequences are false.<\/p>\n<\/blockquote>\n<p>So really you don&#8217;t need to check the length and just check it like this:<\/p>\n<pre><code>% if mydict.get('somekey'):\n    ${mydict['somekey'][0]['hellothere']}\n% endif\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:37:51. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Assuming I have the following in my template: % if not mydict[&#8216;somekey&#8217;] is UNDEFINED: ${mydict[&#8216;somekey&#8217;][0][&#8216;hellothere&#8217;]} % endif My issue is the above does not work as mydict[&#8216;somekey&#8217;] is always an array, but it could be empty. I want to be able to check to make sure that if mydict[&#8216;somekey&#8217;] is defined, I can add a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1185","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1185"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1185\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}