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)

5/5 – (1 vote)

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


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



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Solidity Ether Units, Time Units, and Global Variables xSicKxBot 0 1,222 11-17-2022, 02:30 PM
Last Post: xSicKxBot
  [Tut] State Variables in Solidity xSicKxBot 0 1,285 10-09-2022, 12:31 AM
Last Post: xSicKxBot
  [Tut] How to Stop a For Loop in Python xSicKxBot 0 1,397 09-29-2022, 05:12 AM
Last Post: xSicKxBot
  [Tut] Python foreach Loop xSicKxBot 0 1,182 07-03-2022, 04:55 AM
Last Post: xSicKxBot
  [Tut] How to Initialize Multiple Variables to the Same Value in Python? xSicKxBot 0 1,191 06-30-2022, 05:29 PM
Last Post: xSicKxBot
  [Tut] Python One Line While Loop [A Simple Tutorial] xSicKxBot 0 1,273 07-25-2020, 07:34 AM
Last Post: xSicKxBot
  [Tut] Python One Line For Loop Lambda xSicKxBot 0 1,299 07-24-2020, 06:48 AM
Last Post: xSicKxBot
  [Tut] Python One Line For Loop With If xSicKxBot 0 1,234 07-21-2020, 01:05 PM
Last Post: xSicKxBot
  [Tut] How to Test Multiple Variables Against a Value in Python? xSicKxBot 0 1,233 06-23-2020, 05:00 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016