{"id":113701,"date":"2020-06-02T12:54:21","date_gmt":"2020-06-02T12:54:21","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=9173"},"modified":"2020-06-02T12:54:21","modified_gmt":"2020-06-02T12:54:21","slug":"python-join-list-with-underscore-the-most-pythonic-way","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/06\/02\/python-join-list-with-underscore-the-most-pythonic-way\/","title":{"rendered":"Python Join List with Underscore [The Most Pythonic Way]"},"content":{"rendered":"<p class=\"has-background has-luminous-vivid-amber-background-color\"><strong>The <code>'_'.join(list)<\/code> method on the underscore string <code>'_'<\/code> glues together all strings in the <code>list<\/code> using the underscore string as a delimiter&#8212;and returns a new string. For example, the expression <code>'_'.join(['a', 'b', 'c'])<\/code> returns the new string <code>'a_b_c'<\/code>. <\/strong><\/p>\n<h2>Definition and Usage<\/h2>\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>Here&#8217;s a short example:<\/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=\"\">friends = ['Alice', 'Bob', 'Carl'] # Empty delimiter string ''\nprint(''.join(friends))\n# AliceBobCarl # Delimiter string '_'\nprint('_'.join(friends))\n# Alice_Bob_Carl<\/pre>\n<p>The string elements in the list <code>friends<\/code> are concatenated using the delimiter string <code>''<\/code> in the first example and the underscore character <code>'_'<\/code> in the second example.<\/p>\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\/python-list-concatenation-add-vs-inplace-add-vs-extend\/\" target=\"_blank\">Python Concatenation<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/concatenate-lists-in-python\/\" target=\"_blank\">Python How to Concatenate Lists<\/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<p><strong>Syntax<\/strong><\/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<h2>Code Puzzle<\/h2>\n<p>To practice what you&#8217;ve learned so far, feel free to solve the following interactive code puzzle:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/21de9b905d\" width=\"100%\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Guess the output and check if you were right by running the interactive code shell.<\/em><\/p>\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 &#8216;_&#8217;.join(list) method on the underscore string &#8216;_&#8217; glues together all strings in the list using the underscore string as a delimiter&#8212;and returns a new string. For example, the expression &#8216;_&#8217;.join([&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;]) returns the new string &#8216;a_b_c&#8217;. Definition and Usage The string.join(iterable) method joins the string elements in the iterable to a new string [&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-113701","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\/113701","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=113701"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113701\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=113701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=113701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=113701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}