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

for i j in python

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

Here’s an example:

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

Output:

Alice is 18 years old
Bob is 22 years old

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

for i j in enumerate python

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

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

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

Output:

0 Alice
1 Bob
2 Carl

Notice how the loop iteration capture:

  • i=0 and j='Alice',
  • i=1 and j='Bob', and
  • i=2 and j='Carl'.

🌍 Learn More: The enumerate() function in Python.

Python enumerate()

for i j in zip python

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

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

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

Output:

1 9
2 8
3 7

🌍 Learn More: The zip() function in Python.

for i j in list python

The Python expression 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 i and j.

Here’s an example:

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

Output:

1 9
2 8
3 7

for i j k python

The Python expression 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 i and j.

Here’s an example:

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

Output:

1 2 3
4 5 6
7 8 9

for i j in a b python

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

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

Summary

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

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.

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