<?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: XeyyamSherif</title>
    <description>The latest articles on DEV Community by XeyyamSherif (@xeyyamsherif).</description>
    <link>https://dev.to/xeyyamsherif</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%2F653287%2F1af2333f-f66a-4957-b7ed-507f5adc8dfb.jpeg</url>
      <title>DEV Community: XeyyamSherif</title>
      <link>https://dev.to/xeyyamsherif</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xeyyamsherif"/>
    <language>en</language>
    <item>
      <title>Remove elements from list in for loop</title>
      <dc:creator>XeyyamSherif</dc:creator>
      <pubDate>Thu, 10 Feb 2022 21:12:24 +0000</pubDate>
      <link>https://dev.to/xeyyamsherif/remove-elements-from-list-in-for-loop-3f1g</link>
      <guid>https://dev.to/xeyyamsherif/remove-elements-from-list-in-for-loop-3f1g</guid>
      <description>&lt;p&gt;We have list like,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We want to delete elements from the list while iterating over it,based on some conditions , for example you want to delete only even numbers. For this, we need first to create a copy of the list, and then we will iterate over that copied list. Then for each element, we will check if we want to delete this element or not. If yes, then delete that element from the original list using the remove() function. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
for elem in list(nums):
    if elem % 2 == 0:
        nums.remove(elem)
print(nums)
&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;[1, 3, 5, 7, 9, 11, 13]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>loop</category>
      <category>list</category>
    </item>
  </channel>
</rss>
