{"id":113670,"date":"2020-06-01T11:47:57","date_gmt":"2020-06-01T11:47:57","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=9161"},"modified":"2020-06-01T11:47:57","modified_gmt":"2020-06-01T11:47:57","slug":"how-to-join-a-list-of-lists-you-dont-you-flatten-it","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/06\/01\/how-to-join-a-list-of-lists-you-dont-you-flatten-it\/","title":{"rendered":"How to Join a List of Lists? You Don\u2019t. You Flatten It!"},"content":{"rendered":"<p class=\"has-background has-luminous-vivid-amber-background-color\"><strong>Short answer: To flatten your list of list <code>lst<\/code>, use the nested list comprehension <code>[x for l in lst for x in l]<\/code> to iterate over all nested lists (<code>for l in lst<\/code>) and over all their elements (<code>for x in l<\/code>). Each element <code>x<\/code>, you just place in the outer list, unchanged.<\/strong><\/p>\n<p>When browsing <a rel=\"noreferrer noopener\" href=\"https:\/\/stackoverflow.com\/questions\/716477\/join-list-of-lists-in-python\" target=\"_blank\">StackOverflow<\/a>, I stumble upon this question from time to time: &#8220;<em><strong>How to Join a List of Lists?<\/strong><\/em>&#8220;. My first thought is that the asking person is referring to the<a href=\"https:\/\/blog.finxter.com\/python-join-list\/\" target=\"_blank\" rel=\"noreferrer noopener\"> join() function<\/a> that converts an iterable (such as a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\">list<\/a>) to a string by <a href=\"https:\/\/blog.finxter.com\/python-list-concatenation-add-vs-inplace-add-vs-extend\/\" target=\"_blank\" rel=\"noreferrer noopener\">concatenating <\/a>its elements. <\/p>\n<p>But nothing can be further from the truth! The question is usually about &#8220;flattening&#8221; the list: to transform a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" target=\"_blank\">nested list of lists<\/a> such as <code>[[1, 2, 3], [4, 5, 6]]<\/code> to a flat list <code>[1, 2, 3, 4, 5, 6]<\/code>. So, the real question is:<\/p>\n<h2>How to Flatten a List of Lists in Python?<\/h2>\n<p><strong>Problem<\/strong>: Given a <a href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\">list of lists<\/a>. How to flatten the list of lists by getting rid of the inner lists&#8212;and keeping their elements?<\/p>\n<p><strong>Example<\/strong>: You want to transform a given list into a flat list like here:<\/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 = [[2, 2], [4], [1, 2, 3, 4], [1, 2, 3]] # ... Flatten the list here ... print(lst)\n# [2, 2, 4, 1, 2, 3, 4, 1, 2, 3]<\/pre>\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/flatten-1024x576.jpg\" alt=\"Flatten a List of Lists with List Comprehension\" class=\"wp-image-8068\" width=\"512\" height=\"288\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/flatten-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/flatten-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/flatten-768x432.jpg 768w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/figure>\n<p><strong>Solution<\/strong>: Use a nested <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/which-is-faster-list-comprehension-or-map-function-in-python\/\" target=\"_blank\">list comprehension<\/a> statement <code>[x for l in lst for x in l]<\/code> to flatten 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=\"\">lst = [[2, 2], [4], [1, 2, 3, 4], [1, 2, 3]] # ... Flatten the list here ...\nlst = [x for l in lst for x in l] print(lst)\n# [2, 2, 4, 1, 2, 3, 4, 1, 2, 3]<\/pre>\n<p><strong>Explanation<\/strong>: In the nested list comprehension statement <code>[x for l in lst for x in l]<\/code>, you first iterate over all lists in the list of lists (<code>for l in lst<\/code>). Then, you iterate over all elements in the current list (<code>for x in l<\/code>). This element, you just place in the outer list, unchanged, by using it in the &#8220;expression&#8221; part of the list comprehension statement <code>[<strong>x<\/strong> for l in lst for x in l]<\/code>. <\/p>\n<p><strong>Try It Yourself<\/strong>: You can execute this code snippet yourself in our interactive Python shell. Just click &#8220;Run&#8221; and test the output of this code. <\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/repl.it\/@finxter\/flattenlistoflist?lite=true\" scrolling=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" sandbox=\"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals\" width=\"100%\" height=\"700px\" frameborder=\"no\"><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: How to flatten a three-dimensional list (= a list of lists of lists)? Try it in the shell!<\/em><\/p>\n<h2>Video: List Comprehension Python List of Lists<\/h2>\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=\"List Comprehension Python List of Lists\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/3CGmDLOiqR4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p>Watch the video to learn three ways how to apply <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">list comprehension<\/a> to a list of lists:<\/p>\n<ul>\n<li>to <strong>flatten <\/strong>a list of lists,<\/li>\n<li>to <strong>create <\/strong>a list of lists, and<\/li>\n<li>to <strong>iterate over<\/strong> a list of lists.<\/li>\n<\/ul>\n<p>Well, if you really want to learn how to <code>join()<\/code> a list in Python, visit <a href=\"https:\/\/blog.finxter.com\/python-join-list\/\" target=\"_blank\" rel=\"noreferrer noopener\">this detailed tutorial on the Finxter blog<\/a>.<\/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>Short answer: To flatten your list of list lst, use the nested list comprehension [x for l in lst for x in l] to iterate over all nested lists (for l in lst) and over all their elements (for x in l). Each element x, you just place in the outer list, unchanged. When browsing [&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-113670","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\/113670","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=113670"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113670\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=113670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=113670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=113670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}