{"id":113954,"date":"2020-06-09T07:13:57","date_gmt":"2020-06-09T07:13:57","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=9400"},"modified":"2020-06-09T07:13:57","modified_gmt":"2020-06-09T07:13:57","slug":"the-most-pythonic-way-to-join-a-list-in-reverse-order","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/06\/09\/the-most-pythonic-way-to-join-a-list-in-reverse-order\/","title":{"rendered":"The Most Pythonic Way to Join a List in Reverse Order"},"content":{"rendered":"<p class=\"has-background has-luminous-vivid-amber-background-color\"><strong>The most efficient method to join a list of Python strings in reverse order is to use the Python code <code>''.join(l[::-1])<\/code>. First, reverse the list <code>l<\/code> using slicing with a negative step size <code>l[::-1]<\/code>. Second, glue together the strings in the list using the <code>join(...)<\/code> method on the empty string <code>''<\/code>.<\/strong><\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"The Most Pythonic Way to Join a List in Reverse Order\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/brhppRcRpis?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p><strong>Problem<\/strong>: Given a list of strings. How to join the strings in reverse order?<\/p>\n<p><strong>Example<\/strong>: You want to join the following list<\/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=\"\">l = ['n', 'o', 'h', 't', 'y', 'p']<\/pre>\n<p>to obtain the joined string in reverse order<\/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=\"\">python<\/pre>\n<p>Let&#8217;s get a short overview on how to accomplish this.<\/p>\n<p><strong>Solution Overview<\/strong>: You can try the three methods in our interactive Python shell.<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/8a6ba00cf3\" width=\"100%\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<p>Next, we&#8217;ll dive into each method separately.<\/p>\n<h2>Method 1: Join + Slicing<\/h2>\n<p>The first and most Pythonic method to reverse and join a list of strings is to use slicing with a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/daily-python-puzzle-negative-indexing\/\" target=\"_blank\">negative step size<\/a>. <\/p>\n<p>You can slice any list in Python using the notation <code>list[start:stop:step]<\/code> to create a sublist starting with index <code>start<\/code>, ending right before index <code>stop<\/code>, and using the given <code>step<\/code> size&#8212;which can also be negative to slice from right to left. If you need a refresher on slicing, check out our <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" target=\"_blank\">detailed Finxter blog tutorial<\/a> or our focused book <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/coffee-break-python\/\" target=\"_blank\">&#8220;Coffee Break Python Slicing&#8221;<\/a>.<\/p>\n<p>Here&#8217;s the first method to reverse a list of strings and join the elements together:<\/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=\"\">l = ['n', 'o', 'h', 't', 'y', 'p'] # Method 1\nprint(''.join(l[::-1]))\n# python<\/pre>\n<p>The <code>string.join(iterable)<\/code> method joins the string elements in the <code>iterable<\/code> to a new string by using the <code>string<\/code> on which it is called as a delimiter. <\/p>\n<p>You can call this method on each list object in Python. Here&#8217;s the syntax:<\/p>\n<p><code><code>string.join(iterable)<\/code><\/code><\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Argument<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>iterable<\/code><\/td>\n<td>The elements to be concatenated.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p><strong>Related articles:<\/strong><\/p>\n<ul>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-join-list\/\" target=\"_blank\">Python Join List [Ultimate Guide]<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" target=\"_blank\">Python Slicing [Ultimate Guide]<\/a><\/li>\n<li><a href=\"https:\/\/blog.finxter.com\/python-list-reverse\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Reverse a Python List<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">The Ultimate Guide to Python Lists<\/a><\/li>\n<\/ul>\n<h2>Method 2: Join + reversed()<\/h2>\n<p>The second method is also quite Pythonic: using the <code>reversed()<\/code> built-in function rather than slicing with a negative step size. I&#8217;d say that beginners generally prefer this method because it&#8217;s more readable to them&#8212;while expert coders prefer slicing because it&#8217;s more concise and slightly more efficient.<\/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=\"\">l = ['n', 'o', 'h', 't', 'y', 'p']\nprint(''.join(reversed(l)))\n# python<\/pre>\n<p>The join method glues together all strings in the list&#8212;in <em>reversed<\/em> order!<\/p>\n<h2>Method 3: Simple Loop<\/h2>\n<p>The third method is the least Pythonic one: using a <a href=\"https:\/\/blog.finxter.com\/python-loops\/\">loop <\/a>where it&#8217;s not really needed. Anyways, especially coders coming from other <a href=\"https:\/\/blog.finxter.com\/python-vs-go-which-language-you-should-choose\/\" target=\"_blank\" rel=\"noreferrer noopener\">programming languages<\/a> like Java or C++ will often use this approach.<\/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=\"\">l = ['n', 'o', 'h', 't', 'y', 'p'] s = ''\nfor x in reversed(l): s += x\nprint(s)\n# python\n<\/pre>\n<p>However, there are several disadvantages. Can you see them?<\/p>\n<ul>\n<li>The code is less concise.<\/li>\n<li>The code is less efficient because of the repeated string concatenation. Each loop execution causes the creation of a new string, which is highly inefficient.<\/li>\n<li>The code requires the definition of two new variables <code>x<\/code> and <code>s<\/code> and introduces a higher level of <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/complexity-of-python-operations\/\" target=\"_blank\">complexity<\/a>.<\/li>\n<\/ul>\n<p>You can see this in our interactive memory visualizer:<\/p>\n<p> <iframe loading=\"lazy\" width=\"800\" height=\"500\" frameborder=\"0\" src=\"https:\/\/pythontutor.com\/iframe-embed.html#code=l%20%3D%20%5B'n',%20'o',%20'h',%20't',%20'y',%20'p'%5D%0As%20%3D%20''%0Afor%20x%20in%20reversed%28l%29%3A%0A%20%20%20%20s%20%2B%3D%20x%0Aprint%28s%29%0A&#038;codeDivHeight=400&#038;codeDivWidth=350&#038;cumulative=false&#038;curInstr=0&#038;heapPrimitives=nevernest&#038;origin=opt-frontend.js&#038;py=3&#038;rawInputLstJSON=%5B%5D&#038;textReferences=false\"> <\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: click &#8220;Next&#8221; to see the memory objects used in this code snippet!<\/em><\/p>\n<p><strong>Related articles:<\/strong><\/p>\n<ul>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-profilers-how-to-speed-up-your-python-app\/\" target=\"_blank\">Performance Tuning in Python<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-concatenation-add-vs-inplace-add-vs-extend\/\" target=\"_blank\">List Concatenation<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-built-in-string-functions-simply-explained\/\" target=\"_blank\">Python Built-in String Functions<\/a><\/li>\n<\/ul>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The most efficient method to join a list of Python strings in reverse order is to use the Python code &#8221;.join(l[::-1]). First, reverse the list l using slicing with a negative step size l[::-1]. Second, glue together the strings in the list using the join(&#8230;) method on the empty string &#8221;. Problem: Given a list [&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-113954","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\/113954","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=113954"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113954\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=113954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=113954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=113954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}