How to use For loop for multiple variables in Python

Python programming offers different loops that manage looping requirements. Python provides three statements or methods to execute loops. There are for loop, while loop, and nested loop. Although all three methods have the same essential functions, they differ in syntax and the time required to check conditions.

One of the most used loops is “for loop”. When an iterator comes into the picture, for loop is very useful performing various operations in each element of the iterator.

Python for loop

A for loop can be used to iterate over an array (that can be either the form of a list or a tuple, dictionary, a set of strings). This is different from the “for keyword” found in other programming languages and is more of an iterator technique used in other object-oriented programming languages.

Using the for loop, you can execute a series of statements, each time for each element of iterators such as list set, tuple, etc. For example, let’s define a list and iterate each element using for loop.

cars = ["bmw", "audi", "jaguar"]

for car in cars:
   print(car)

Output

bmw
audi
jaguar

In this example, we defined a list of cars. The list contains three elements, and to print all the elements one by one, we used a for loop. We only took one variable car and printed it on the Python console. Let’s see how to use for loop to handle multiple variables.

For loop for multiple variables Python

Various combinations of methods can be used with multiple variables using for loop.

  1. Using zip() function with for loop for multiple variables.
  2. Using for loop with a key-value pair of a dictionary.
  3. Using enumerate() method with for loop using multiple variables.

Using zip() function with for loop for multiple variables

The zip() is a built-in Python function that takes iterables, aggregates them in a tuple, and returns them. This zip() function is utilized for parallel interactions and simultaneously unpacking multiple variables.

cars = ["bmw", "audi", "jaguar"]
prices = [49999, 40000, 69999]

for car, price in zip(cars, prices):
   print(f"{car} car price is ${price}")

Output

bmw car price is $49999
audi car price is $40000
jaguar car price is $69999

In this example, the zip() function takes two lists and provides an iterable that gives a tuple of the corresponding elements of both lists. Using for loop, we iterated two lists which you can say as multiple variables.

Using for loop with a key-value pair of a dictionary

There is only one data type whose structure involves a key-value pair. It is a dictionary. A dictionary maps one value to another.

cars = {"bmw": 49999, "audi": 40000, "jaguar": 69999}

for car, price in cars.items():
   print(f"{car} car price is ${price}")

Output

bmw car price is $49999
audi car price is $40000
jaguar car price is $69999

Using for loop, we use multiple variables in a dictionary.

Using enumerate() with for loop using multiple variables

The enumerate() is a built-in Python method that adds a counter to an iterable and returns it in the form of enumerating object. This enumerated object can then be used directly for loops or converted into a list of tuples using the list() method.

cars = ["bmw", "audi", "jaguar"]
prices = [49999, 40000, 69999]

for i, car in enumerate(cars):
   price = prices[i]
   print(f"{car} car price is ${price}")

Output

bmw car price is $49999
audi car price is $40000
jaguar car price is $69999

In this example, we took cars and prices as two lists and did the assignment on both lists. The enumerate object provides the indexes, which is excellent and makes looping over the two lists simultaneously an achievable task.

That’s it for this tutorial.

Leave a Comment