{"id":114219,"date":"2020-06-16T10:19:20","date_gmt":"2020-06-16T10:19:20","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=9794"},"modified":"2020-06-16T10:19:20","modified_gmt":"2020-06-16T10:19:20","slug":"how-to-use-pythons-join-on-a-list-of-objects-not-strings","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/06\/16\/how-to-use-pythons-join-on-a-list-of-objects-not-strings\/","title":{"rendered":"How to Use Python\u2019s Join() on a List of Objects (Not Strings)?"},"content":{"rendered":"<p class=\"has-background has-luminous-vivid-amber-background-color\"><strong>The most Pythonic way to concatenate a list of objects is the expression <code>''.join(str(x) for x in lst)<\/code> that converts each object to a string using the built-in <code>str(...)<\/code> function in a generator expression. You can concatenate the resulting list of strings using the <code>join()<\/code> method on the empty string as a delimiter. The result is a single string value that&#8217;s the concatenation of the objects&#8217; string representations.<\/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=\"What&#039;s The Most Pythonic Way to Concatenate a List of Objects into a String?\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/_JDhMYSIDWI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p>Writing Pythonic code is at the heart of being an effective coder&#8212;and if you choose the wrong ways of solving a problem, you&#8217;ll open yourself up for criticism and struggles throughout your <a href=\"https:\/\/blog.finxter.com\/programming-your-intelligence\/\" target=\"_blank\" rel=\"noreferrer noopener\">programming career<\/a>. So, what&#8217;s the most Pythonic way of solving the following problem?<\/p>\n<p><strong>Problem<\/strong>: Given a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">list<\/a> of objects. How to <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/concatenate-lists-in-python\/\" target=\"_blank\">concatenate <\/a>the string representations of those objects?<\/p>\n<p><strong>Example<\/strong>: You have the following list of objects.<\/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=\"\">class Obj: def __init__(self, val): self.val = val def __str__(self): return str(self.val) lst = [Obj(0), Obj(1), Obj(2), Obj(4)]<\/pre>\n<p>You want to obtain the following result of concatenated string <a href=\"https:\/\/blog.finxter.com\/python-join-list-of-unicode-strings\/\" target=\"_blank\" rel=\"noreferrer noopener\">representations <\/a>of the objects in the 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=\"\">0124<\/pre>\n<p>Want to play? Run the following interactive code shell:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/0b30e74dbc\" width=\"100%\" height=\"600\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Run the interactive code snippet. Which method do you like most?<\/em><\/p>\n<h2>Method 1: Join + List Comprehension + Str<\/h2>\n<p>This method uses three Python features.<\/p>\n<p>First, the <code>string.join(iterable)<\/code> method expects an <code>iterable<\/code> of strings as input and concatenates those using the <code>string<\/code> delimiter. <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-join-list\/\" target=\"_blank\">You can read more about the<code> join()<\/code> function in our ultimate blog guide.<\/a><\/p>\n<p>Second, list comprehension is the Pythonic way to create Python lists in a single line. You use the syntax <code>[expression + statement]<\/code>. <\/p>\n<ul>\n<li>In the <code>expression<\/code>, you define how each element should be modified before adding it into a new list. <\/li>\n<li>In the <code>statement<\/code>, you define which elements to modify and add to the list in the first place. <\/li>\n<\/ul>\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">You can read more about list comprehension in the detailed Finxter guide on the topic.<\/a><\/p>\n<p>Third, the <code>str(...)<\/code> function converts any Python object into a string representation. If you define your custom objects, you should overwrite the <code>__str__()<\/code> method to customize how exactly your objects are represented as strings.<\/p>\n<p>Combining those three features results in the following simple solution to concatenate the string representations of a list of objects.<\/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=\"\">print(''.join([str(x) for x in lst]))\n# 0124<\/pre>\n<p>But there&#8217;s a slight simplification lurking around. Read on to learn which!<\/p>\n<h2>Method 2: Join + Generator Expression + Str<\/h2>\n<p>The previous method has shown a quite effective way to concatenate the string representations of some objects using the <code>join()<\/code> function of Python strings. As the <code>join()<\/code> function expects a <a href=\"https:\/\/blog.finxter.com\/python-list-to-string\/\" target=\"_blank\" rel=\"noreferrer noopener\">list of strings<\/a>, you need to convert all objects <code>x<\/code> into plain strings using the<code> str(x)<\/code> function. <\/p>\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/pythonjoinlistobject-1024x576.jpg\" alt=\"Concatenate string representations of list of objects with join() function and generator expression.\" class=\"wp-image-9810\" width=\"768\" height=\"432\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/pythonjoinlistobject-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/pythonjoinlistobject-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/pythonjoinlistobject-768x432.jpg 768w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n<p>However, there&#8217;s no need to create a separate list in which you store the string representations. Converting one <a href=\"https:\/\/blog.finxter.com\/object-oriented-programming-terminology-cheat-sheet\/\" target=\"_blank\" rel=\"noreferrer noopener\">object <\/a>at a time is enough because the join function needs only an iterable as an input&#8212;and not necessarily a <a href=\"https:\/\/blog.finxter.com\/python-list-methods-cheat-sheet-instant-pdf-download\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python list<\/a>. <em>(All Python lists are iterables but not all iterables are Python lists.)<\/em><\/p>\n<p>To free up the memory, you can use a <a href=\"https:\/\/blog.finxter.com\/how-to-use-generator-expressions-in-python-dictionaries\/\" target=\"_blank\" rel=\"noreferrer noopener\">generator expression<\/a> (without the square brackets needed to create a 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=\"\">print(''.join(str(x) for x in lst))\n# 0124<\/pre>\n<p>In contrast to Method 1, this ensures that the original list does not exist in memory twice&#8212;once as a list of objects and once as a list of string represenations of those exact objects. Therefore, this method can be considered <a href=\"https:\/\/blog.finxter.com\/whats-the-best-pep8-python-style-checker\/\" target=\"_blank\" rel=\"noreferrer noopener\">the most Pythonic<\/a> one.<\/p>\n<h2>Method 3: Join + Generator Expression + Custom String Representation<\/h2>\n<p>A slight modification of the previous version is to use your own custom string representation&#8212;rather than the one implemented by the <code>__str__<\/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=\"\">print(''.join(str(x.val) for x in lst))\n# 0124<\/pre>\n<p>This gives you some flexibility how you can represent each object for your specific application.<\/p>\n<h2>Method 4: Join + Map + Lambda<\/h2>\n<p>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> 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<p><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/a-simple-introduction-of-the-lambda-function-in-python\/\" target=\"_blank\">Lambda functions<\/a> are anonymous functions that are not defined in the <a href=\"https:\/\/blog.finxter.com\/python-namespaces-made-simple\/\" target=\"_blank\" rel=\"noreferrer noopener\">namespace <\/a>(they have no names). The syntax is: <code>lambda &lt;argument name(s)> : &lt;return expression><\/code>. You&#8217;ll see an example next, where each function argument is mapped to its string representation.<\/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=\"\">print(''.join(map(lambda x: str(x), lst)))\n# 0124<\/pre>\n<p>This method is a more functional programming style&#8212;and some Python coders prefer that. However, Python&#8217;s creator <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/about-guidos-fate-of-reduce-in-python-3000\/\" target=\"_blank\">Guido van Rossum preferred list comprehension over functional programming<\/a> because of the readability. That&#8217;s why Methods 1-3 should be preferred in general. <\/p>\n<p>If you absolutely want to take functional programming, use the next version:<\/p>\n<h2>Method 5: Join + Map + Str<\/h2>\n<p>There&#8217;s no need to use the lambda function to transform each list element to a string representation&#8212;if there&#8217;s a built-in function that&#8217;s already doing exactly this: the <code>str(...)<\/code> function you&#8217;ve already seen! <\/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=\"\">print(''.join(map(str, lst)))\n# 0124<\/pre>\n<p>Because of its <a href=\"https:\/\/blog.finxter.com\/10-python-one-liners\/\" target=\"_blank\" rel=\"noreferrer noopener\">conciseness and elegance<\/a>, passing the <code>str<\/code> function name as a function argument into the <code>map<\/code> function is the most Pythonic <em>functional <\/em>way of solving this problem.<\/p>\n<h2>Method 6: Simple Loop + Str (Naive)<\/h2>\n<p>Sure, you can also solve the problem in a non-Pythonic way by using a simple <a href=\"https:\/\/blog.finxter.com\/python-one-line-for-loop-a-simple-tutorial\/\" target=\"_blank\" rel=\"noreferrer noopener\">for loop<\/a> and build up the string:<\/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=\"\">s = ''\nfor x in lst: s += str(x)\nprint(s)\n# 0124<\/pre>\n<p>But that&#8217;s not only less <a href=\"https:\/\/pythononeliners.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">concise<\/a>, it&#8217;s also less efficient. So, a master coder in Python would never use such a method!<\/p>\n<p>If you want to become a Python master coder, check out my <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/subscribe\/\" target=\"_blank\">free in-depth Python email course<\/a>. Join tens of thousands of ambitious Python coders and become a Python master the automatic way!<\/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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The most Pythonic way to concatenate a list of objects is the expression &#8221;.join(str(x) for x in lst) that converts each object to a string using the built-in str(&#8230;) function in a generator expression. You can concatenate the resulting list of strings using the join() method on the empty string as a delimiter. The result [&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-114219","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\/114219","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=114219"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/114219\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=114219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=114219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=114219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}