{"id":82,"date":"2010-09-14T19:04:28","date_gmt":"2010-09-15T00:04:28","guid":{"rendered":"http:\/\/soljerome.com\/blog\/?p=82"},"modified":"2010-09-14T19:51:56","modified_gmt":"2010-09-15T00:51:56","slug":"python-lists-in-reverse","status":"publish","type":"post","link":"https:\/\/www.soljerome.com\/blog\/2010\/09\/14\/python-lists-in-reverse\/","title":{"rendered":"Python lists in reverse"},"content":{"rendered":"<p>I was recently converting a bash script to python. I had a need to grab the last item (and only the last item) off the end of a list in order to implement bash&#8217;s <code>basename<\/code> function since <a href=\"http:\/\/docs.python.org\/library\/os.path.html#os.path.basename\" target=\"_blank\">Python&#8217;s basename function<\/a> is not quite the same. The bash script had a line like the following<br \/>\n<code><br \/>\ntmpbase=`basename $0`<br \/>\n<\/code><br \/>\nI was able to get the information I needed by using the <code>__file__<\/code> attribute in the script itself. From this, I was able to split the full pathname like this:<\/p>\n<pre>solj@abbysplaything $ cat foo.py\r\n#!\/usr\/bin\/env python3\r\nprint(__file__.split('\/'))\r\n\r\nsolj@abbysplaything $ python \/home\/solj\/foo.py\r\n['', 'home', 'solj', 'foo.py']<\/pre>\n<p>As you can tell, the length of this path could vary depending on where the user runs the script from. Therefore, I needed to grab the first item from the <strong>end<\/strong> of the list in order to properly emulate the basename function of bash. I ended up being able to do the following:<br \/>\n<code><br \/>\ntmpbase =  __file__.split('\/')[-1:]<br \/>\n<\/code><br \/>\nThe negative index allows you to count from the end of the list (I love Python). However, as it turns out, I am blind and didn&#8217;t finish fully reading the os.path documentation. This particular problem was solved in a much more elegant way using <a href=\"http:\/\/docs.python.org\/library\/os.path.html#os.path.split\" target=\"_blank\">os.path.split()<\/a> although I find the negative index to be an extremely useful thing to know.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was recently converting a bash script to python. I had a need to grab the last item (and only the last item) off the end of a list in order to implement bash&#8217;s basename function since Python&#8217;s basename function is not quite the same. The bash script had a line like the following tmpbase=`basename <a href='https:\/\/www.soljerome.com\/blog\/2010\/09\/14\/python-lists-in-reverse\/' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/posts\/82"}],"collection":[{"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":3,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/posts\/82\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}