Sick Gaming
[Tut] For Loop with Two Variables (for i j in python) - Printable Version

+- Sick Gaming (https://sickgaming.net)
+-- Forum: Programming (https://sickgaming.net/forum-76.html)
+--- Forum: Python (https://sickgaming.net/forum-83.html)
+--- Thread: [Tut] For Loop with Two Variables (for i j in python) (/thread-99764.html)



[Tut] For Loop with Two Variables (for i j in python) - xSicKxBot - 08-03-2022

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

5/5 – (1 vote)

for i j in python

The Python expression
Code:
for i, j in XXX
allows you to iterate over an iterable
Code:
XXX
of pairs of values. For example, to iterate over a list of tuples, this expression captures both tuple values in the loop variables
Code:
i
and
Code:
j
at once in each iteration.

Here’s an example:

Code:
for i, j in [('Alice', 18), ('Bob', 22)]: print(i, 'is', j, 'years old')

Output:

Code:
Alice is 18 years old Bob is 22 years old

Notice how the first loop iteration captures
Code:
i='Alice'
and
Code:
j=18
, whereas the second loop iteration captures
Code:
i='Bob'
and
Code:
j=22
.

for i j in enumerate python

The Python expression
Code:
for i, j in enumerate(iterable)
allows you to loop over an iterable using variable
Code:
i
as a counter (0, 1, 2, …) and variable
Code:
j
to capture the iterable elements.

Here’s an example where we assign the counter values
Code:
i=0,1,2
to the three elements in
Code:
lst
:

Code:
lst = ['Alice', 'Bob', 'Carl'] for i, j in enumerate(lst): print(i, j)

Output:

Code:
0 Alice 1 Bob 2 Carl

Notice how the loop iteration capture:
  • Code:
    i=0
    and
    Code:
    j='Alice'
    ,

  • Code:
    i=1
    and
    Code:
    j='Bob'
    , and

  • Code:
    i=2
    and
    Code:
    j='Carl'
    .

[Image: 1f30d.png] Learn More: The
Code:
enumerate()
function in Python.

[Image: image-117.png]

for i j in zip python

The Python expression
Code:
for i, j in zip(iter_1, iter_2)
allows you to align the values of two iterables
Code:
iter_1
and
Code:
iter_2
in an ordered manner and iterate over the pairs of elements. We capture the two elements at the same positions in variables
Code:
i
and
Code:
j
.

Here’s an example that zips together the two lists
Code:
[1,2,3]
and
Code:
[9,8,7,6,5]
.

Code:
for i, j in zip([1,2,3], [9,8,7,6,5]): print(i, j)

Output:

Code:
1 9 2 8 3 7

[Image: 1f30d.png] Learn More: The
Code:
zip()
function in Python.

[Image: image-118.png]

for i j in list python

The Python expression
Code:
for i, j in list
allows you to iterate over a given list of pairs of elements (list of tuples or list of lists). In each iteration, this expression captures both pairs of elements at once in the loop variables
Code:
i
and
Code:
j
.

Here’s an example:

Code:
for i, j in [(1,9), (2,8), (3,7)]: print(i, j)

Output:

Code:
1 9 2 8 3 7
for i j k python

The Python expression
Code:
for i, j, k in iterable
allows you to iterate over a given list of triplets of elements (list of tuples or list of lists). In each iteration, this expression captures all three elements at once in the loop variables
Code:
i
and
Code:
j
.

Here’s an example:

Code:
for i, j, k in [(1,2,3), (4,5,6), (7,8,9)]: print(i, j, k)

Output:

Code:
1 2 3 4 5 6 7 8 9
for i j in a b python

Given two lists
Code:
a
and
Code:
b
, you can iterate over both lists at once by using the expression
Code:
for i,j in zip(a,b)
.

Given one list
Code:
['a', 'b']
, you can use the expression
Code:
for i,j in enumerate(['a', 'b'])
to iterate over the pairs of (identifier, list element) tuples.

Summary

The Python
Code:
for
loop is a powerful method to iterate over multiple iterables at once, usually with the help of the
Code:
zip()
or
Code:
enumerate()
functions.

Code:
for i, j in zip(range(10), range(10)): # (0,0), (1,1), ..., (9,9)

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.

Code:
for i,j,k in [(1,2,3), (4,5,6)]: # Do Something