[Tut] List Comprehension Python List of Lists - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] List Comprehension Python List of Lists (/thread-94823.html) |
[Tut] List Comprehension Python List of Lists - xSicKxBot - 05-01-2020 List Comprehension Python List of Lists <div><p>[<strong>20-SEC SUMMARY] Given a list of list stored in variable <code>lst</code>. </strong></p> <ul> <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> <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> </ul> <figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"> <div class="wp-block-embed__wrapper"> <div class="ast-oembed-container"><iframe 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> </div> </figure> <p>List comprehension is a compact way of creating lists. The simple formula is <code>[ expression + context ]</code>.</p> <ul> <li><strong>Expression</strong>: What to do with each list element?</li> <li><strong>Context</strong>: What list elements to select? It consists of an arbitrary number of for and if statements.</li> </ul> <p>The example <code>[x for x in range(3)]</code> creates the list <code>[0, 1, 2]</code>.</p> <p>In this tutorial, you’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> <ul> <li>to <strong>flatten </strong>a list of lists</li> <li>to <strong>create </strong>a list of lists</li> <li>to <strong>iterate over</strong> a list of lists</li> </ul> <p>Additionally, you’ll learn how to apply nested list comprehension. So let’s get started!</p> <h2>Python List Comprehension Flatten List of Lists</h2> <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—and keeping their elements?</p> <p><strong>Example</strong>: You want to transform a given list into a flat list like here:</p> <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) # [2, 2, 4, 1, 2, 3, 4, 1, 2, 3]</pre> <figure class="wp-block-image size-large is-resized"><img 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="(max-width: 512px) 100vw, 512px" /></figure> <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> <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 ... lst = [x for l in lst for x in l] print(lst) # [2, 2, 4, 1, 2, 3, 4, 1, 2, 3]</pre> <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 “expression” part of the list comprehension statement <code>[<strong>x</strong> for l in lst for x in l]</code>. </p> <p><strong>Try It Yourself</strong>: You can execute this code snippet yourself in our interactive Python shell. Just click “Run” and test the output of this code. </p> <p> <iframe 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> <p>Can you flatten a three-dimensional list (= a list of lists of lists)? Try it in the shell!</p> <h2>Python List Comprehension Create List of Lists</h2> <p><strong>Problem</strong>: How to create a list of lists by modifying each element of an original list of lists?</p> <p><strong>Example</strong>: You’re given the list</p> <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> <p>You want to add one to each element and create a new list of lists:</p> <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> <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> <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]] new = [[x+1 for x in l] for l in lst] print(new) # [[2, 3, 4], [5, 6, 7], [8, 9, 10]]</pre> <p><strong>Explanation</strong>: You’ll study more examples of two nested list comprehension statements later. The main idea is to use as “expression” 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> <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 “Next” button repeatedly to see what happens in each step of the code.</p> <p> <iframe 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&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe> </p> <p>Let’s explore the third question: how to use list comprehension to iterate over a list of lists?</p> <h2>Python List Comprehension Over List of Lists</h2> <p>You’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> <h2>How Does Nested List Comprehension Work in Python?</h2> <figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"> <div class="wp-block-embed__wrapper"> <div class="ast-oembed-container"><iframe 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> </div> </figure> <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> <ul> <li>Coming from a computer science background, I was assuming that “nested list comprehension” refers to the creation of a list of lists. In other words: <strong>How to create a nested list with list comprehension?</strong></li> <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> </ul> <h3>How to create a nested list with list comprehension?</h3> <p>It is possible to create a nested list with list comprehension in Python. What is a nested list? It’s 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> <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 lst = [[x for x in range(5)] for y in range(3)] print(lst) # [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]</pre> <p>As you can see, we create a list with three elements. Each list element is a list by itself. </p> <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> <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> <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) print(l) # [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] # ... and not [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]]</pre> <h3>How to use a nested for loop in the list comprehension?</h3> <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>‘[‘ + expression + context + ‘]’</code>)? </p> <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> <p>In the expression, you can use any variable you define within a for loop in the context. Let’s have a look at an example.</p> <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> <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 users = ["John", "Alice", "Ann", "Zach"] pairs = [] for x in users: for y in users: if x != y: pairs.append((x,y)) print(pairs) #[('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> <p>Now, this code is a mess! How can we fix it? Simply use nested list comprehension!</p> <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 pairs = [(x,y) for x in users for y in users if x!=y] print(pairs) # [('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> <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> <h2>Where to Go From Here?</h2> <p>Enough theory, let’s get some practice!</p> <p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p> <p><strong>Practice projects is how you sharpen your saw in coding!</strong></p> <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> <p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p> <p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p> <p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p> </div> https://www.sickgaming.net/blog/2020/04/30/list-comprehension-python-list-of-lists/ |