#Day2 - List/Dictionary Comprehension in Python
Tags: python, programming, tutorial, 100daysofcode
Published: 2021-03-22 18:23:13

<!-- wp:paragraph -->
<p>List/Dictionary Comprehension is an elegant one-liner to iterate over a List/Dictionary. They allow us to create new objects based on the already existing ones.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The syntax for both List and Dictionary Comprehension are pretty similar and although I'll be discussing both, I'll use List comprehension to talk about nest comprehension and conditional comprehension.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 id="the-normal-way-of-iterating-over-a-list-using-for-loops">The 'Normal' way of iterating over a list using For Loops</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let's assume we have a list of numbers and we want to create a new list that stores the squares of the numbers. This is how we would do it using for loops</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1616432411872/zXHNrETAQ.png" alt="image.png"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3 id="list-comprehension">List Comprehension</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Using List Comprehension we could write lines 2-4 in a single line.<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1616432552102/x6R6sMeMu.png" alt="image.png"></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As you can see, it looks much cleaner and elegant.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 id="dictionary-comprehension">Dictionary Comprehension</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let's assume we want to create a dictionary that stores the index as a key and the value at that index as the value.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1616435309257/mglj0hC5u.png" alt="image.png"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3 id="list-comprehension-with-an-if-condition">List Comprehension with an If Condition</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let's say we want to store the square of the number only if it's an even number. We can simply add an if statement after the iterable<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1616432887139/AkUWDqYTt.png" alt="image.png"></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 id="list-comprehension-with-ifelse-condition">List Comprehension with If/Else Condition.</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now, suppose we want to store the squares of even numbers and the cubes of odd numbers.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1616433376693/FJUpLJkDL.png" alt="image.png"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The syntax is slightly different as compared to list comprehension with if statement.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 id="nested-list-comprehension">Nested List Comprehension</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We can have an outer for loop with an inner for loop.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1616433587158/l9obCcanS.png" alt="image.png"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The first for loop would be the outer for loop and the second for loop would be inner for loop. The expression (i,j) would evaluate (0,0) , (0,1) , (0,2).... since j is in the inner for loop.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 id="some-tips">Some tips</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Don't overuse List/Dictionary Comprehension. It is a one-liner for a reason. Although it makes your code look clean, ensure it is readable and can be understood by other developers</li><li>List comprehension doesn't call append during every iteration and in general, they are faster than for loops. However, if you are using a complex function inside your list comprehension, it will affect performance.</li></ul>
<!-- /wp:list -->