{"id":112243,"date":"2020-04-30T08:12:00","date_gmt":"2020-04-30T08:12:00","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=8041"},"modified":"2020-04-30T08:12:00","modified_gmt":"2020-04-30T08:12:00","slug":"list-comprehension-python-list-of-lists","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/04\/30\/list-comprehension-python-list-of-lists\/","title":{"rendered":"List Comprehension Python List of Lists"},"content":{"rendered":"<p>[<strong>20-SEC SUMMARY] Given a list of list stored in variable <code>lst<\/code>. <\/strong><\/p>\n<ul>\n<li><strong>To flatten a list of lists, use the list comprehension statement <code>[x for l in lst for x in l]<\/code>. <\/strong><\/li>\n<li><strong>To modify all elements in a list of lists (e.g., increment them by one), use a list comprehension of list comprehensions <code>[[x+1 for x in l] for l in lst]<\/code>. <\/strong><\/li>\n<\/ul>\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>List comprehension is a compact way of creating lists. The simple formula is <code>[ expression + context ]<\/code>.<\/p>\n<ul>\n<li><strong>Expression<\/strong>: What to do with each list element?<\/li>\n<li><strong>Context<\/strong>: What list elements to select? It consists of an arbitrary number of for and if statements.<\/li>\n<\/ul>\n<p>The example <code>[x for x in range(3)]<\/code> creates the list <code>[0, 1, 2]<\/code>.<\/p>\n<p>In this tutorial, you&#8217;ll learn three ways how to apply <a href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\" rel=\"noreferrer noopener\">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<\/li>\n<li>to <strong>iterate over<\/strong> a list of lists<\/li>\n<\/ul>\n<p>Additionally, you&#8217;ll learn how to apply nested list comprehension. So let&#8217;s get started!<\/p>\n<h2>Python List Comprehension Flatten List of Lists<\/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\" height=\"700px\" width=\"100%\" src=\"https:\/\/repl.it\/@finxter\/flattenlistoflist?lite=true\" scrolling=\"no\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" sandbox=\"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals\"><\/iframe> <\/p>\n<p>Can you flatten a three-dimensional list (= a list of lists of lists)? Try it in the shell!<\/p>\n<h2>Python List Comprehension Create List of Lists<\/h2>\n<p><strong>Problem<\/strong>: How to create a list of lists by modifying each element of an original list of lists?<\/p>\n<p><strong>Example<\/strong>: You&#8217;re given 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=\"\">[[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/pre>\n<p>You want to add one to each element and create a new list of lists:<\/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=\"\">[[2, 3, 4], [5, 6, 7], [8, 9, 10]]<\/pre>\n<p><strong>Solution<\/strong>: Use two nested list comprehension statements, one to create the outer list of lists, and one to create the inner lists.<\/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, 2, 3], [4, 5, 6], [7, 8, 9]]\nnew = [[x+1 for x in l] for l in lst]\nprint(new)\n# [[2, 3, 4], [5, 6, 7], [8, 9, 10]]<\/pre>\n<p><strong>Explanation<\/strong>: You&#8217;ll study more examples of two nested list comprehension statements later. The main idea is to use as &#8220;expression&#8221; of the outer list comprehension statement a list comprehension statement by itself. Remember, you can create any object you want in the expression part of your list comprehension statement. <\/p>\n<p><strong>Explore the code<\/strong>: You can play with the code in the interactive Python tutor that visualizes the execution step-by-step. Just click the &#8220;Next&#8221; button repeatedly to see what happens in each step of the code.<\/p>\n<p> <iframe loading=\"lazy\" width=\"800\" height=\"500\" frameborder=\"0\" src=\"https:\/\/pythontutor.com\/iframe-embed.html#code=lst%20%3D%20%5B%5B1,%202,%203%5D,%20%5B4,%205,%206%5D,%20%5B7,%208,%209%5D%5D%0Anew%20%3D%20%5B%5Bx%2B1%20for%20x%20in%20l%5D%20for%20l%20in%20lst%5D%0Aprint%28new%29&#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>Let&#8217;s explore the third question: how to use list comprehension to iterate over a list of lists?<\/p>\n<h2>Python List Comprehension Over List of Lists<\/h2>\n<p>You&#8217;ve seen this in the previous example where you not only created a list of lists, you also iterated over each element in the list of lists. To summarize, you can iterate over a list of lists by using the statement <code>[[modify(x) for x in l] for l in lst]<\/code> using any statement or <a href=\"https:\/\/blog.finxter.com\/how-to-define-a-function-with-default-arguments-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">function <\/a><code>modify(x)<\/code> that returns an arbitrary <a href=\"https:\/\/blog.finxter.com\/object-oriented-programming-terminology-cheat-sheet\/\" target=\"_blank\" rel=\"noreferrer noopener\">object<\/a>. <\/p>\n<h2>How Does Nested List Comprehension Work in Python?<\/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=\"Python One-Liner Trick 9 - Nested List Comprehension\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/aBC0VhpXkOQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p>After publishing the first version of this tutorial, many <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/subscribe\/\" target=\"_blank\">readers<\/a> asked me to write a follow-up tutorial on nested list comprehension in Python. There are two interpretations of nested list comprehension:<\/p>\n<ul>\n<li>Coming from a computer science background, I was assuming that &#8220;nested list comprehension&#8221; refers to the creation of a list of lists. In other words: <strong>How to create a nested list with list comprehension?<\/strong><\/li>\n<li>But after a bit of research, I learned that there is a second interpretation of nested list comprehension: <strong>How to use a nested for loop in the list comprehension?<\/strong><\/li>\n<\/ul>\n<h3>How to create a nested list with list comprehension?<\/h3>\n<p>It is possible to create a nested list with list comprehension in Python. What is a nested list? It\u2019s a <a href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\">list of lists<\/a>. Here is an 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=\"\">## Nested List Comprehension\nlst = [[x for x in range(5)] for y in range(3)]\nprint(lst)\n# [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]<\/pre>\n<p>As you can see, we create a list with three elements. Each list element is a list by itself. <\/p>\n<p>Everything becomes clear when we go back to our magic formula of list comprehension: <code>[expression + context]<\/code>. The expression part generates a new list consisting of 5 integers. The context part repeats this three times. Hence, each of the three nested lists has five elements. <\/p>\n<p>If you are an advanced programmer (<a href=\"https:\/\/finxter.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">test your skills on the Finxter app<\/a>), you may ask whether there is some aliasing going on here. Aliasing in this context means that the three list elements point to the same list <code>[0, 1, 2, 3, 4]<\/code>. This is not the case because each expression is evaluated separately, a new list is created for each of the three context executions. This is nicely demonstrated in this code snippet:<\/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[0].append(5)\nprint(l)\n# [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]\n# ... and not [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]]<\/pre>\n<h3>How to use a nested for loop in the list comprehension?<\/h3>\n<p>To be frank, the latter one is super-simple stuff. Do you remember the formula of <a href=\"https:\/\/blog.finxter.com\/list-comprehension-python-list-of-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\">list comprehension<\/a> (= <code>\u2018[\u2018 + expression + context + \u2018]\u2019<\/code>)? <\/p>\n<p>The context is an arbitrary complex restriction construct of for loops and if restrictions with the goal of specifying the data items on which the expression should be applied.<\/p>\n<p>In the expression, you can use any variable you define within a for loop in the context. Let\u2019s have a look at an example.<\/p>\n<p>Suppose you want to use list comprehension to make this code more <a href=\"https:\/\/blog.finxter.com\/python-one-liners-the-ultimate-collection\/\" target=\"_blank\" rel=\"noreferrer noopener\">concise <\/a>(for example, you want to find all possible pairs of users in your social network application):<\/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=\"\"># BEFORE\nusers = [\"John\", \"Alice\", \"Ann\", \"Zach\"]\npairs = []\nfor x in users: for y in users: if x != y: pairs.append((x,y))\nprint(pairs)\n#[('John', 'Alice'), ('John', 'Ann'), ('John', 'Zach'), ('Alice', 'John'), ('Alice', 'Ann'), ('Alice', 'Zach'), ('Ann', 'John'), ('Ann', 'Alice'), ('Ann', 'Zach'), ('Zach', 'John'), ('Zach', 'Alice'), ('Zach', 'Ann')]<\/pre>\n<p>Now, this code is a mess! How can we fix it? Simply use nested list comprehension!<\/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=\"\"># AFTER\npairs = [(x,y) for x in users for y in users if x!=y]\nprint(pairs)\n# [('John', 'Alice'), ('John', 'Ann'), ('John', 'Zach'), ('Alice', 'John'), ('Alice', 'Ann'), ('Alice', 'Zach'), ('Ann', 'John'), ('Ann', 'Alice'), ('Ann', 'Zach'), ('Zach', 'John'), ('Zach', 'Alice'), ('Zach', 'Ann')]<\/pre>\n<p>As you can see, we are doing exactly the same thing as with un-nested list comprehension. The only difference is to write the two <a href=\"https:\/\/blog.finxter.com\/python-one-line-for-loop-a-simple-tutorial\/\" target=\"_blank\" rel=\"noreferrer noopener\">for loops<\/a> and the<a href=\"https:\/\/blog.finxter.com\/if-then-else-in-one-line-python\/\" target=\"_blank\" rel=\"noreferrer noopener\"> if statement in a single line<\/a> within the list notation <code>[]<\/code>.<\/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>[20-SEC SUMMARY] Given a list of list stored in variable lst. To flatten a list of lists, use the list comprehension statement [x for l in lst for x in l]. To modify all elements in a list of lists (e.g., increment them by one), use a list comprehension of list comprehensions [[x+1 for x [&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-112243","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\/112243","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=112243"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/112243\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=112243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=112243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=112243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}