<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mozart Maia</title>
    <description>The latest articles on DEV Community by Mozart Maia (@mozamaia).</description>
    <link>https://dev.to/mozamaia</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F687819%2F848240ac-f7cd-4892-975c-498696b3970d.jpeg</url>
      <title>DEV Community: Mozart Maia</title>
      <link>https://dev.to/mozamaia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mozamaia"/>
    <language>en</language>
    <item>
      <title>Python's Secret Weapon: List Comprehension</title>
      <dc:creator>Mozart Maia</dc:creator>
      <pubDate>Mon, 29 Apr 2024 20:33:00 +0000</pubDate>
      <link>https://dev.to/mozamaia/pythons-secret-weapon-list-comprehension-5bjn</link>
      <guid>https://dev.to/mozamaia/pythons-secret-weapon-list-comprehension-5bjn</guid>
      <description>&lt;p&gt;If you are used to make programs or scripts in Python, you know the language has many ways to deal with collections. It's possible to iterate over a list (access the elements within a list) using for loops by index, as shown here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftocnyinzm7l9mapo3ia2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftocnyinzm7l9mapo3ia2.png" alt="for loop in python" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another way to do this with basically the same results is with a while loop:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkqlx0v8hskeb3oenrlvw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkqlx0v8hskeb3oenrlvw.png" alt="while loop in python" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the only thing you need is the elements of the list, without the index, you can iterate using a simpler syntax:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F56mvlm9lgvklsjigsopp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F56mvlm9lgvklsjigsopp.png" alt="for loop in python without index" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The while loop in most cases might be slower than the for loops, because its running other statements, like the increment of variable &lt;em&gt;counter&lt;/em&gt; and checking if the &lt;em&gt;counter&lt;/em&gt; is still smaller than the size of &lt;em&gt;my_list&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;However, python offers another method to iterate over lists faster than both and without using libraries like Numpy. This is &lt;strong&gt;List Comprehensions&lt;/strong&gt;. &lt;a href="https://www.freecodecamp.org/news/list-comprehension-in-python/#:~:text=List%20comprehension%20is%20an%20easy%20to%20read%2C%20compact%2C%20and%20elegant%20way%20of%20creating%20a%20list%20from%20any%20existing%20iterable%20object.%20Basically%2C%20it%27s%20a%20simpler%20way%20to%20create%20a%20new%20list%20from%20the%20values%20in%20a%20list%20you%20already%20have"&gt;In this article from freeCodeCamp&lt;/a&gt;, we have a great definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;List comprehension is an easy to read, compact, and elegant way of creating a list from any existing iterable object. Basically, it's a simpler way to create a new list from the values in a list you already have.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I couldn't say it better. List comprehensions can be a bit confusing for new programmers, especially those who have only learned imperative programming, because it uses &lt;a href="https://en.wikipedia.org/wiki/Declarative_programming"&gt;declarative paradigm&lt;/a&gt;. I won't go into details of declarative/functional programming paradigms, but they are interesting ways of programming and it's worth exploring them as well.&lt;/p&gt;

&lt;p&gt;So how we do this list comprehension thing? Let's break it down step by step with this image:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4tu7zra4v9f5jyupzsab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4tu7zra4v9f5jyupzsab.png" alt="list comprehension in python" width="800" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First we have a square bracket, the beginning of the list we are generating. Then in blue we have the elements of our list and any transformations we want to do with them, in this case we are multiplying each one for two.&lt;/p&gt;

&lt;p&gt;Second in yellow we have the for loop where we will iterate over a list. We could use a variable holding a list, but to keep simple I created one using the function range(10), which give me a sequence from 0 to 10 (excluding the last).&lt;/p&gt;

&lt;p&gt;Finally, in green is the condition we want to apply in the elements of the list. In this case, you can think of it as filtering our list, where we are verifying if the division of x by 2 (using the modulo operator - %) is equal to zero. In other words, if the number is even.&lt;/p&gt;

&lt;p&gt;The result of this is the following list:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzd5dfoc9nmsehfj6uxvn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzd5dfoc9nmsehfj6uxvn.png" alt="result of list comprehension" width="800" height="87"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As a final exercise, let's create a simple program to find out if a number is prime or not using list comprehension. If you want, you can try it on your own and then check after if your answer is similar to mine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbgbu4be6iymqoe3tykcf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbgbu4be6iymqoe3tykcf.png" alt="Image description" width="800" height="609"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you liked this little article. Happy coding! :)&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>learning</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
