<?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: Mariem Sghaier</title>
    <description>The latest articles on DEV Community by Mariem Sghaier (@mariem_sghaier).</description>
    <link>https://dev.to/mariem_sghaier</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%2F1843072%2F44d0f8fa-221e-4d4b-8762-34a205d1be49.jpg</url>
      <title>DEV Community: Mariem Sghaier</title>
      <link>https://dev.to/mariem_sghaier</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mariem_sghaier"/>
    <language>en</language>
    <item>
      <title>Another test post content</title>
      <dc:creator>Mariem Sghaier</dc:creator>
      <pubDate>Mon, 13 Jan 2025 12:39:28 +0000</pubDate>
      <link>https://dev.to/mariem_sghaier/another-test-post-content-567d</link>
      <guid>https://dev.to/mariem_sghaier/another-test-post-content-567d</guid>
      <description></description>
    </item>
    <item>
      <title>Another test post content</title>
      <dc:creator>Mariem Sghaier</dc:creator>
      <pubDate>Sat, 11 Jan 2025 22:32:19 +0000</pubDate>
      <link>https://dev.to/mariem_sghaier/another-test-post-content-1a46</link>
      <guid>https://dev.to/mariem_sghaier/another-test-post-content-1a46</guid>
      <description></description>
      <category>test</category>
    </item>
    <item>
      <title>This is a small test post!</title>
      <dc:creator>Mariem Sghaier</dc:creator>
      <pubDate>Sat, 11 Jan 2025 22:26:26 +0000</pubDate>
      <link>https://dev.to/mariem_sghaier/this-is-a-small-test-post-4g6i</link>
      <guid>https://dev.to/mariem_sghaier/this-is-a-small-test-post-4g6i</guid>
      <description></description>
      <category>test</category>
    </item>
    <item>
      <title>This is a small test post</title>
      <dc:creator>Mariem Sghaier</dc:creator>
      <pubDate>Sat, 11 Jan 2025 21:58:53 +0000</pubDate>
      <link>https://dev.to/mariem_sghaier/this-is-a-small-test-post-50kb</link>
      <guid>https://dev.to/mariem_sghaier/this-is-a-small-test-post-50kb</guid>
      <description></description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Another test post content</title>
      <dc:creator>Mariem Sghaier</dc:creator>
      <pubDate>Sat, 11 Jan 2025 21:57:42 +0000</pubDate>
      <link>https://dev.to/mariem_sghaier/another-test-post-content-5g64</link>
      <guid>https://dev.to/mariem_sghaier/another-test-post-content-5g64</guid>
      <description></description>
      <category>test</category>
    </item>
    <item>
      <title>Testing with different text</title>
      <dc:creator>Mariem Sghaier</dc:creator>
      <pubDate>Sat, 11 Jan 2025 21:49:41 +0000</pubDate>
      <link>https://dev.to/mariem_sghaier/testing-with-different-text-2edn</link>
      <guid>https://dev.to/mariem_sghaier/testing-with-different-text-2edn</guid>
      <description></description>
      <category>testing</category>
    </item>
    <item>
      <title>An elegant and simple way to use Python Lists: List Comprehensions</title>
      <dc:creator>Mariem Sghaier</dc:creator>
      <pubDate>Fri, 27 Sep 2024 23:33:04 +0000</pubDate>
      <link>https://dev.to/mariem_sghaier/an-elegant-and-simple-way-to-use-python-lists-list-comprehensions-584j</link>
      <guid>https://dev.to/mariem_sghaier/an-elegant-and-simple-way-to-use-python-lists-list-comprehensions-584j</guid>
      <description>&lt;p&gt;You might think &lt;strong&gt;List Comprehension&lt;/strong&gt; is an advanced concept. However, it can simplify your code with just &lt;strong&gt;one line&lt;/strong&gt; in tricky situations. It's time to understand how it works. I will explain it at a beginner level with &lt;strong&gt;examples&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Exactly is List Comprehension?
&lt;/h2&gt;

&lt;p&gt;You often see the notation &lt;code&gt;l2 = [x+1 for x in l]&lt;/code&gt;. It's said to be the same as this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;l2 = []
for x in l:
    x = x + 1
l2.append(x)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For both cases, if we start with &lt;code&gt;l = [10, 100, 1000]&lt;/code&gt;, l2 will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[11, 101, 1001]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first syntax is what we call &lt;strong&gt;list comprehension&lt;/strong&gt;.&lt;br&gt;
You may prefer the usual for loop, but by the end of this article, I promise you’ll be confident using list comprehension!&lt;/p&gt;

&lt;p&gt;In addition, let's check the detailed official definition from the documentation &lt;a href="https://dev.tourl"&gt;https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's break it down more. List comprehension is just a different way to write code that is shorter and easier to make a new list. The result of list comprehension is a list, which is why we assign it to a list.&lt;br&gt;
Let's go over the second part of the definition, which discusses common examples. This should be done using specific examples.&lt;br&gt;
&lt;strong&gt;1.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fast_foods = ["Burger", "Pizza", "Tacos", "Fried Chicken", "Hot Dog"]
uppercase_fast_foods = [food.upper() for food in fast_foods]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this, uppercase_fast_foods will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;['BURGER', 'PIZZA', 'TACOS', 'FRIED CHICKEN', 'HOT DOG']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the upper() function to change each food item in our list to uppercase. As a result, all items are now in uppercase. This is how we 'make new lists where each element is the result of some operations applied to each member of another sequence or iterable.'&lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fast_foods = ["Burger", "Pizza", "Tacos", "Fried Chicken", "Hot Dog"]
foods_with_space = [food for food in fast_foods if " " in food]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this, foods_with_space will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;['Fried Chicken', 'Hot Dog']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The line of code above retrieves the items from the list that contain a whitespace character. This is how we 'make new lists where each element is the result of some operations applied to each member of another sequence or iterable.'&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples of List Comprehension
&lt;/h2&gt;

&lt;p&gt;I’ve used list comprehensions in many problems because whenever I encountered too many for-loops, I thought, 'No problem, I’ll just simplify them.' Turns out, it’s the same logic, just cleaner! 😄&lt;br&gt;
Without further ado, here are some of the most relevant examples I’ve come up with:&lt;br&gt;
&lt;strong&gt;1. Modify each element of the list&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foods = ["Burger", "Fries", "Fried Chicken", "Hot Dog", "Pizza"]
foods_with_version = [food + ' - 2024' for food in foods]
print(foods_with_version)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;['Burger - 2024', 'Fries - 2024', 'Fried Chicken - 2024', 'Hot Dog - 2024', 'Pizza - 2024']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we take a list of food items and add '- 2024' to each one. We use list comprehension to quickly create a new list with these updated names.&lt;br&gt;
&lt;strong&gt;2. Make a sublist from a list based on a condition&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foods = ["Burger", "Fried Chicken", "Hot Dog", "Fries", "Pizza"]
long_foods = [food for food in foods if len(food) &amp;gt; 7]
print(long_foods)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;['Fried Chicken']`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we create a list of food items and filter out the ones that have more than 7 characters. We use list comprehension with a condition to achieve this.&lt;br&gt;
&lt;strong&gt;3. Use the range function with list comprehension to create a list&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = [i for i in range(10, 20, 2)]
print(x)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[10, 12, 14, 16, 18]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we create a list of numbers ranging from 10 to 18 using list comprehension with range().&lt;br&gt;
&lt;strong&gt;4. Apply list comprehension to a string&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;input_string = "hello world"
marked_vowels = ['*' if char in 'aeiouAEIOU' else char for char in input_string]
print(marked_vowels)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;['h', '*', 'l', 'l', '*', ' ', 'w', 'o', 'r', 'l', 'd']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this last example, we take a string and mark its vowels with an asterisk (*). We use list comprehension to create a new list based on the original string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Throughout this article, I’ve covered all the basic insights about list comprehensions, from the definition to various examples that explain them further. I hope everything is clear, and you feel more motivated to incorporate list comprehensions into your Python code from now on!&lt;/p&gt;

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