<?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: nadbag</title>
    <description>The latest articles on DEV Community by nadbag (@nadbag).</description>
    <link>https://dev.to/nadbag</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%2F164689%2F81984d9e-9afa-4440-b929-e73c00e1279a.png</url>
      <title>DEV Community: nadbag</title>
      <link>https://dev.to/nadbag</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nadbag"/>
    <language>en</language>
    <item>
      <title>Mastering Python Lists: Essential Techniques You Need to Know</title>
      <dc:creator>nadbag</dc:creator>
      <pubDate>Thu, 07 Nov 2024 06:29:18 +0000</pubDate>
      <link>https://dev.to/nadbag/different-ways-to-use-python-list-5ang</link>
      <guid>https://dev.to/nadbag/different-ways-to-use-python-list-5ang</guid>
      <description>&lt;p&gt;Discover Python's versatile ways to iterate over lists, from basic loops to advanced comprehensions. Master these techniques to write cleaner and more efficient code!&lt;/p&gt;

&lt;h2&gt;
  
  
  For
&lt;/h2&gt;

&lt;h3&gt;
  
  
  simple for
&lt;/h3&gt;

&lt;p&gt;This will loop through the list and each element of the list will be available as a variable in every iteration. This is widely used when there is a need to go over all the elements of the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;operating_systems = ["windows", "mac", "linux"]
for os in operating_systems:
    print(os)`

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output
windows
mac
linux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  for and range
&lt;/h3&gt;

&lt;p&gt;When there is need for accessing based on index and index value is required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;operating_systems = ["windows", "mac", "linux"]
for i in range(len(operating_systems)):
    print(f"Index {i}: {operating_systems[i]}")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output
Index 0: windows
Index 1: mac
Index 2: linux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  for and enumerate
&lt;/h3&gt;

&lt;p&gt;This is an elegant way, if you need both index and the value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;operating_systems = ["windows", "mac", "linux"]
for index, os in enumerate(operating_systems):
    print(f"Index is {index} and value is {os}")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output
Index is 0 and value is windows
Index is 1 and value is mac
Index is 2 and value is linux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  While
&lt;/h2&gt;

&lt;h3&gt;
  
  
  simple while
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;operating_systems = ["windows", "mac", "linux"]
i = 0 # Inital condition, required to start
while i &amp;lt; len(operating_systems):
    print(f"While looping {i} got the value {operating_systems[i]}")
    i = i + 1 # This is very important, dont forget about infinite loops
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output
While looping 0 got the value windows
While looping 1 got the value mac
While looping 2 got the value linux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Iterator
&lt;/h2&gt;

&lt;p&gt;Gives fine control over when to move the iterator forward, though we have to rely on the StopIteration to check if the end is reached.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;operating_systems = ["windows", "mac", "linux"]
iterator = iter(operating_systems)
while True:
    try:
        os = next(iterator)
        print(f"Consumed form iterator {os}")
    except StopIteration:
        print("Consumed all from iterator")
        break
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output
Consumed form iterator windows
Consumed form iterator mac
Consumed form iterator linux
Consumed all from iterator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Hack to avoid StopIteration
iterator = iter(operating_systems)
end_of_list = object()
reached_end = False
while not reached_end:
    os = next(iterator, end_of_list)# a predefined object as end of the list
    if os != end_of_list:
        print(os)
    else:
        reached_end = True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  List comprehension
&lt;/h2&gt;

&lt;p&gt;When transformation is required&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;operating_systems = ["windows", "mac", "linux"]
os_uppercase = [os.upper() for os in operating_systems]
print(os_uppercase) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output
['WINDOWS', 'MAC', 'LINUX']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Cycling
&lt;/h2&gt;

&lt;p&gt;When cycling through a list is require. Use with proper boundary condition to break the loop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import itertools
operating_systems = ["windows", "mac", "linux"]
for item in itertools.cycle(operating_systems):  
    print(item)
# Infinite cycling loopmake sure to have proper boundary condition to break
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output
windows
mac
linux
windows
mac
linux
windows
mac
linux
windows
mac
linux
windows ....... Infinite loop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Over multiple lists
&lt;/h2&gt;

&lt;p&gt;Simultaneously loop over multiple lists. Note the output if the list sizes are different.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;operating_systems = ["windows", "mac", "linux"]
mobile_operating_systems = ["android", "ios"]

for os, mobile_os in zip(operating_systems,mobile_operating_systems):
    print(os, mobile_os)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output
windows android
mac ios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Loop in reverse
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;operating_systems = ["windows", "mac", "linux"]
for reversed_os in reversed(operating_systems):
    print(reversed_os)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output
linux
mac
windows
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>programming</category>
      <category>datastructures</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
