For loop to print multiple variables in Python

Python is the simplest programming language that is secure and has easy syntax. It fulfills the developer requirements within a small piece of code then how can it be possible that it does not allow …

print multiple variables in Python

Python is the simplest programming language that is secure and has easy syntax. It fulfills the developer requirements within a small piece of code then how can it be possible that it does not allow to print multiple variables simultaneously by using loops? In this article, we are showing you how to print multiple variables by using for loop in python programming.

Below are the few methods with their example that help you to understand the concept of print multiple variables in for loop.

  • enumeration() function
  • zip function
  • zip_longest function

Before explaining that how a for loop works with different functions to print multiple variables we will take a look at print a list by for loop.

In this example, we stored inside a variable named list_1 that prints a single list by using for loop.

list_1=["a", "b", "c", "d", "e"]
for i in list_1:varible 
    print(i)

output

Below are the methods of how to Iterate over multiple variables by using for loop.

i. Enumeration() Function (for loop with two variables in python)

There is a built-in method enumeration that helps to return more than one variable in the output. It works as print a list in the corresponding of another list. But the drawback of this function is, it can only work with two lists at a time. 

Here is an example of an enumeration() function

SmartPhone = ["Samsung M21","Apple SE","Huawei 8i","Oppo A16","Vivo Y53s"]
prices =[284, 399, 330, 140, 255]
for i, phone in enumerate(SmartPhone):
    price = prices[i]
    print(f"{phone}: ${price}" )

output

ii. Zip() Method

There is another built-in method called zip () that is used to return the iterator. Zip() function returns output until the smallest list is completely used up. itertools is a module of python 3 libraries that perform the computational task efficiently over alterable datasets.

import itertools
rank = [1, 2, 3, 4, 5]
phone = ["Samsung", "Apple", "Huawei", "Oppo", "Vivo"]
price = [284, 399, 330]

# Iterates over 3 lists and executes
# Execute till minimum list ends up.
# 2 times as len(price)= 2 that is shortest length list
for (a, b, c) in zip(rank, phone , price):
    print(f" {a}. {b} ${c}")

output

It runs until the shortest list ends and returns output that is equivalent to the length of the shortest list such as len(price) = 3. So, it is not a good choice if the developer has lists of different lengths and the program stop when the shortest list ends up. 

iii. itertools.zip_longest()

Use itertools.zip_longest() method that prints all lists simultaneously until the longest list is completely used up. This is the most efficient way to print multiple lists, assignments, and variables by using for loop.  It creates an iterator that combines all items from given lists and interacts with them to unpack in the output.

Here is an example of itertools.zip_longest() function.

import itertools
rank = [1, 2, 3, 4, 5]
phone = ["Samsung", "Apple", "Huawei", "Oppo", "Vivo"]
price = [284, 399, 330]

# Iterates over 3 lists and executes
# Execute till maximum list among all the three.
# 3 times as len(rank, phone)= 5 that is longest length lists
for (a, b, c) in itertools.zip_longest(rank, phone , price):
    print(f" {a}. {b} ${c}")

output

Conclusion

In this article, we used two built-in methods to iterate multiple variables by using for loop: enumeration() function and zip() method. Enumeration used inside for loop prints two variables and the zip() method runs until the smallest list is completely used up but the most effective way to pass multiple variables over a for loop is itertools.zip_longest() method that run till last longest list ends up.

Suggested Read: How to Change the Language on Twitter

Leave a Comment