{"id":110871,"date":"2020-03-28T07:33:18","date_gmt":"2020-03-28T07:33:18","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=7132"},"modified":"2020-03-28T07:33:18","modified_gmt":"2020-03-28T07:33:18","slug":"python-one-liners-the-ultimate-collection","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/03\/28\/python-one-liners-the-ultimate-collection\/","title":{"rendered":"Python One-Liners \u2013 The Ultimate Collection"},"content":{"rendered":"<p>This resource is meant to be the ultimate collection of <a href=\"https:\/\/www.amazon.com\/gp\/product\/B07ZY7XMX8\" target=\"_blank\" rel=\"noreferrer noopener\">Python One-Liners<\/a>. If you have an idea for a one-liner to be published here, send me a message at chris (at) finxter.com. <\/p>\n<h2>Find All Indices of an Element in a List<\/h2>\n<p>Say, you want to do the same as the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-index\/\" target=\"_blank\">list.index(element)<\/a> method but return all indices of the element in the list rather than only a single one. <\/p>\n<p>In this one-liner, you&#8217;re looking for element <code>'Alice'<\/code> in the list <code>[1, 2, 3]<\/code> so it even works if the element is not in the list (unlike the <code>list.index()<\/code> method). <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lst = [1, 2, 3]\nindices = [i for i in range(len(lst)) if lst[i]=='Alice']\nindex = indices[0] if indices else None\nprint(index)<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This resource is meant to be the ultimate collection of Python One-Liners. If you have an idea for a one-liner to be published here, send me a message at chris (at) finxter.com. Find All Indices of an Element in a List Say, you want to do the same as the list.index(element) method but return all [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-110871","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/110871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=110871"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/110871\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=110871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=110871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=110871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}