{"id":113773,"date":"2020-06-04T08:40:18","date_gmt":"2020-06-04T08:40:18","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=9230"},"modified":"2020-06-04T08:40:18","modified_gmt":"2020-06-04T08:40:18","slug":"the-most-pythonic-way-to-convert-a-list-of-tuples-to-a-string","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/06\/04\/the-most-pythonic-way-to-convert-a-list-of-tuples-to-a-string\/","title":{"rendered":"The Most Pythonic Way to Convert a List of Tuples to a String"},"content":{"rendered":"<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/convert-1024x576.jpg\" alt=\"Convert List of Tuples to String\" class=\"wp-image-9242\" width=\"768\" height=\"432\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/convert-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/convert-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/convert-768x432.jpg 768w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n<p class=\"has-background has-luminous-vivid-amber-background-color\"><strong>The most Pythonic way to convert a list of tuples to a string is to use the built-in method <code>str(...)<\/code>. If you want to customize the delimiter string, the most Pythonic way is to concatenate the <code>join()<\/code> method and the <code>map()<\/code> function <code>'\\n'.join(map(str, lst))<\/code> to convert all tuples to strings and gluing those together with the new-line delimiter <code>'\\n'<\/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 Convert a List of Tuples to a String\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/TRlWAz4DGQc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p><em><strong>Exercise<\/strong>: Run the interactive code snippet. Which method do you like most?<\/em><\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/ad8db38a29\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen=\"\" width=\"100%\" height=\"356\" frameborder=\"0\"><\/iframe> <\/p>\n<h2>Method 1: Default String Conversion<\/h2>\n<p>Say, you&#8217;ve got a list of tuples, and you want to convert it to a string (e.g., see <a rel=\"noreferrer noopener\" href=\"https:\/\/stackoverflow.com\/questions\/3292643\/python-convert-list-of-tuples-to-string\" target=\"_blank\">this <\/a>SO post). The easiest way to accomplish this is to use the default string conversion method <code>str(...)<\/code>. <\/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,1), (2,1), (4,2)]\n>>> str(lst) '[(1, 1), (2, 1), (4, 2)]'<\/pre>\n<p>The result is a nicely formatted string representation of your list of tuples.<\/p>\n<h2>Method 2: Join() and Map()<\/h2>\n<p>However, if you want to customize the <a href=\"https:\/\/blog.finxter.com\/python-join-list\/\" target=\"_blank\" rel=\"noreferrer noopener\">delimiter <\/a>string, you can use the <code>join()<\/code> method in combination with the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-get-rid-of-pythons-map-function-with-list-comprehension\/\" target=\"_blank\"><code>map()<\/code> function<\/a>:<\/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,1), (2,1), (4,2)] print('--'.join(map(str, lst)))\n# (1, 1)--(2, 1)--(4, 2)<\/pre>\n<p>The <code>map()<\/code> function transforms each tuple into a string value, and the <code>join()<\/code> method transforms the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-join-list\/\" target=\"_blank\">collection <\/a>of strings to a single string&#8212;using the given delimiter <code>'--'<\/code>. If you forget to transform each tuple into a string with the <code>map()<\/code> function, you&#8217;ll get a <code>TypeError<\/code> because the <code>join()<\/code> method expects a collection of strings. <\/p>\n<h2>Method 3: Flatten List of Tuples<\/h2>\n<p>If you want to <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/join-list-of-lists\/\" target=\"_blank\">flatten the list<\/a> and integrate all <a href=\"https:\/\/blog.finxter.com\/how-to-convert-list-of-tuples-to-list-of-lists-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">tuple <\/a>elements into a single large collection of elements, you can use a simple <a href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\" rel=\"noreferrer noopener\">list comprehension<\/a> statement <code>[str(x) for t in lst for x in t]<\/code>. <\/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,1), (2,1), (4,2)] print('\\n'.join([str(x) for t in lst for x in t])) '''\n1\n1\n2\n1\n4\n2 '''<\/pre>\n<p>If you want to redefine <a href=\"https:\/\/blog.finxter.com\/the-separator-and-end-arguments-of-the-python-print-function\/\" target=\"_blank\" rel=\"noreferrer noopener\">how to print each tuple<\/a>&#8212;for example, separating all tuple values by a single whitespace character&#8212;use the following method based on a combination of the <code>join()<\/code> method and the <code>map()<\/code> function with a custom lambda function <code>lambda x: str(x[0]) + ' ' + str(x[1])<\/code> to be applied to each list element. <\/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,1), (2,1), (4,2)] print('\\n'.join(map(lambda x: str(x[0]) + ' ' + str(x[1]), lst))) '''\n1 1\n2 1\n4 2 '''<\/pre>\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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The most Pythonic way to convert a list of tuples to a string is to use the built-in method str(&#8230;). If you want to customize the delimiter string, the most Pythonic way is to concatenate the join() method and the map() function &#8216;\\n&#8217;.join(map(str, lst)) to convert all tuples to strings and gluing those together with [&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-113773","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\/113773","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=113773"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113773\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=113773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=113773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=113773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}