Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] For Loop with Two Variables (for i j in python)

#1
For Loop with Two Variables (for i j in python)

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;528311&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2>for i j in python</h2>
<p class="has-global-color-8-background-color has-background">The Python expression <code>for i, j in XXX</code> allows you to iterate over an <a href="https://blog.finxter.com/iterators-iterables-and-itertools/" data-type="post" data-id="29507" target="_blank" rel="noreferrer noopener">iterable</a> <code>XXX</code> of pairs of values. For example, to iterate over a list of tuples, this expression captures both <a href="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" data-type="post" data-id="12043" target="_blank" rel="noreferrer noopener">tuple</a> values in the loop variables <code>i</code> and <code>j</code> at once in each iteration. </p>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i, j in [('Alice', 18), ('Bob', 22)]: print(i, 'is', j, 'years old')</pre>
<p>Output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Alice is 18 years old
Bob is 22 years old</pre>
<p>Notice how the first loop iteration captures <code>i='Alice'</code> and <code>j=18</code>, whereas the second loop iteration captures <code>i='Bob'</code> and <code>j=22</code>. </p>
<h2>for i j in enumerate python</h2>
<p class="has-global-color-8-background-color has-background">The Python expression <code>for i, j in enumerate(iterable)</code> allows you to loop over an iterable using variable <code>i</code> as a counter (0, 1, 2, …) and variable <code>j</code> to capture the iterable elements.</p>
<p>Here’s an example where we assign the counter values <code>i=0,1,2</code> to the three elements in <code>lst</code>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lst = ['Alice', 'Bob', 'Carl']
for i, j in enumerate(lst): print(i, j)
</pre>
<p>Output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">0 Alice
1 Bob
2 Carl</pre>
<p>Notice how the loop iteration capture:</p>
<ul>
<li><code>i=0</code> and <code>j='Alice'</code>,</li>
<li><code>i=1</code> and <code>j='Bob'</code>, and</li>
<li><code>i=2</code> and <code>j='Carl'</code>.</li>
</ul>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Learn More</strong>: The <code><a href="https://blog.finxter.com/python-enumerate/" data-type="post" data-id="20466" target="_blank" rel="noreferrer noopener">enumerate()</a></code> function in Python.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://blog.finxter.com/python-enumerate/" target="_blank" rel="noreferrer noopener"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-117.png" alt="Python enumerate()" class="wp-image-528379" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-117.png 1024w, https://blog.finxter.com/wp-content/uplo...00x169.png 300w, https://blog.finxter.com/wp-content/uplo...68x432.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>
</div>
<h2>for i j in zip python</h2>
<p class="has-global-color-8-background-color has-background">The Python expression <code>for i, j in zip(iter_1, iter_2)</code> allows you to align the values of two iterables <code>iter_1</code> and <code>iter_2</code> in an ordered manner and iterate over the pairs of elements. We capture the two elements at the same positions in variables <code>i</code> and <code>j</code>.</p>
<p>Here’s an example that zips together the two <a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332">li</a><a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank" rel="noreferrer noopener">s</a><a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332">ts</a> <code>[1,2,3]</code> and <code>[9,8,7,6,5]</code>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">
for i, j in zip([1,2,3], [9,8,7,6,5]): print(i, j)
</pre>
<p>Output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 9
2 8
3 7</pre>
</p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Learn More</strong>: The <code><a href="https://blog.finxter.com/python-ziiiiiiip-a-helpful-guide/" data-type="post" data-id="1938" target="_blank" rel="noreferrer noopener">zip()</a></code> function in Python.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://blog.finxter.com/python-ziiiiiiip-a-helpful-guide/" target="_blank" rel="noreferrer noopener"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-118.png" alt="" class="wp-image-528457" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-118.png 1024w, https://blog.finxter.com/wp-content/uplo...00x169.png 300w, https://blog.finxter.com/wp-content/uplo...68x432.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>
</div>
<h2>for i j in list python</h2>
<p class="has-global-color-8-background-color has-background">The Python expression <code>for i, j in list</code> allows you to iterate over a given list of pairs of elements (<a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-merge-lists-into-a-list-of-tuples/" data-type="post" data-id="9506" target="_blank">list of tuples</a> or <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890" target="_blank">list of lists</a>). In each iteration, this expression captures both pairs of elements at once in the loop variables <code>i</code> and <code>j</code>. </p>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i, j in [(1,9), (2,8), (3,7)]: print(i, j)</pre>
<p>Output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 9
2 8
3 7</pre>
<h2>for i j k python</h2>
<p class="has-global-color-8-background-color has-background">The Python expression <code>for i, j, k in iterable</code> allows you to iterate over a given list of triplets of elements (<a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-merge-lists-into-a-list-of-tuples/" data-type="post" data-id="9506" target="_blank">list of tuples</a> or <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890" target="_blank">list of lists</a>). In each iteration, this expression captures all three elements at once in the loop variables <code>i</code> and <code>j</code>. </p>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i, j, k in [(1,2,3), (4,5,6), (7,8,9)]: print(i, j, k)</pre>
<p>Output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 2 3
4 5 6
7 8 9</pre>
<h2>for i j in a b python</h2>
<p>Given two lists <code>a</code> and <code>b</code>, you can iterate over both lists at once by using the expression <code>for i,j in zip(a,b)</code>. </p>
<p>Given one list <code>['a', 'b']</code>, you can use the expression <code>for i,j in enumerate(['a', 'b'])</code> to iterate over the pairs of (identifier, list element) <a href="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" data-type="post" data-id="12043" target="_blank" rel="noreferrer noopener">tuples</a>. </p>
<h2>Summary</h2>
<p>The Python <code>for</code> loop is a powerful method to iterate over multiple iterables at once, usually with the help of the <code>zip()</code> or <code>enumerate()</code> functions.</p>
<pre class="wp-block-preformatted"><code>for i, j in zip(range(10), range(10)): # (0,0), (1,1), ..., (9,9)</code></pre>
<p>If a list element is an iterable by itself, you can capture all iterable elements using a comma-separated list when defining the loop variables.</p>
<pre class="wp-block-preformatted"><code>for i,j,k in [(1,2,3), (4,5,6)]: # Do Something</code></pre>
</div>


https://www.sickgaming.net/blog/2022/07/...in-python/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016