Use a for Loop for Multiple Variables in Python

Vaibhhav Khetarpal Dec 21, 2022 Jul 02, 2021
  1. Use the for Loop for Multiple Assignments in a Dictionary in Python
  2. Use the enumerate() Function for Multiple Assignments in a List in Python
  3. Use the zip() Function for Multiple Assignments in a Tuple or a List in Python
Use a for Loop for Multiple Variables in Python

A for loop is used for iterating over any sequence, from a list to a tuple to a dictionary. It can even iterate over a string. This article discusses how to use the for loop for multiple variables in Python.

The use of multiple variables in a for loop in Python can be applied to lists or dictionaries, but it does not work for a general error. These multiple assignments of variables simultaneously, in the same line of code, are known as iterable unpacking.

Use the for Loop for Multiple Assignments in a Dictionary in Python

A dictionary can be utilized to store the data values in key-value pairs. In simple terms, A dictionary maps one value to another, similar to how an English dictionary is used to map one word to its definition.

Here, we will use the items() method on the given Python dictionary. This function provides the output as a list that contains all the dictionary keys with their values.

The following code uses the for loop for multiple assignments in a dictionary.

dict1 = {1: "Bitcoin", 2: "Ethereum"}
for key, value in dict1.items():
    print(f"Key {key} has value {value}")

Output:

Key 1 has value Bitcoin
Key 2 has value Ethereum

In the code above, we also use the f-strings method along with the print function to implement the for loop and display the key-value pairs.

Use the enumerate() Function for Multiple Assignments in a List in Python

The enumerate() function makes any collection into an enumerated object and returns it. This method can be used when, say, we have two lists, and we’re going to work on both at the same time with the help of indexes to look for corresponding elements in the other list.

The following code uses the enumerate() function for multiple assignments in a list.

coins = ["Bitcoin", "Ethereum", "Cardano"]
prices = [48000,2585,2]
for i, coin in enumerate(coins):
    price = prices[i]
    print(f"${price} for 1 {coin}")

Output:

$48000 for 1 Bitcoin
$2585 for 1 Ethereum
$2 for 1 Cardano

Here, we take two lists, namely coins and prices, and simultaneously do the assignment on both of the lists. The enumerate object provides the indexes, which is great and makes looping over the two lists simultaneously an achievable task.

Use the zip() Function for Multiple Assignments in a Tuple or a List in Python

The zip() function is a built-in function offered in Python and is utilized to create an iterator that will interact with and combine elements from two or more given iterables.

The zip() function can be used for parallel interaction and can also make unpacking several variables at a time possible. The following code uses the zip() function for multiple assignments in a tuple or a list.

coins = ["Bitcoin", "Ethereum", "Cardano"]
prices = [48000,2585,2]
for coin, price in zip(coins, prices):
    print(f"${price} for 1 {coin}")

Output:

$48000 for 1 Bitcoin
$2585 for 1 Ethereum
$2 for 1 Cardano

Here, the zip function takes in two lists and provides an iterable that gives a tuple of the corresponding elements of both the lists as we loop over it.

Vaibhhav Khetarpal avatar Vaibhhav Khetarpal avatar

Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.

LinkedIn

Related Article - Python Loop