Home / Fundamentals

# How to iterate through two lists in parallel

Iterating through two lists in parallel means that you want to loop through both lists at the same time and perform some operation on each pair of elements. There are several ways to do this in Python, and the method you choose will depend on your specific needs and the structure of your lists.

## Method 1: Using the zip function

One of the simplest and most efficient ways to iterate through two lists in parallel is to use the built-in zip function. This function takes two or more iterables and returns an iterator that combines their elements into tuples. You can then loop through the iterator and unpack the tuples into separate variables.

Here is an example of how to use zip to iterate through two lists and print the sum of each pair of elements:

```list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]

for x, y in zip(list1, list2):
print(x + y)
```
```6
8
10
12
```

Note that zip will only iterate as far as the shortest list, so if the lists are of different lengths, the longer list will be truncated.

## Method 2: Using the enumerate function

Another option is to use the built-in enumerate function, which returns an iterator that returns tuples containing the index and value of each element in a list. You can use this function to loop through one list, and use the index to access the corresponding element in the other list.

Here is an example of how to use enumerate to iterate through list1 and access the corresponding elements in list2:

```list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]

for i, x in enumerate(list1):
y = list2[i]
print(x + y)
```
```6
8
10
12
```

## Method 3: Using a range object

If you know the length of the lists in advance, you can use a range object to loop through the lists by index. This method is similar to the previous one, but it allows you to avoid calling len on the lists and potentially creating unnecessary variables.

Here is an example of how to use a range object to iterate through list1 and list2:

```list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]

for i in range(len(list1)):
x = list1[i]
y = list2[i]
print(x + y)
```
```6
8
10
12
```

## Method 4: Using a while loop (continued)

If you need more control over the loop, you can use a while loop to iterate through the lists. This method allows you to exit the loop early or skip certain iterations, but it requires more code and may be less efficient than the other methods.

Here is an example of how to use a while loop to iterate through list1 and list2:

```list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]

i = 0
while i < len(list1) and i < len(list2):
x = list1[i]
y = list2[i]
print(x + y)
i += 1
```
```6
8
10
12
```

## Method 5: Using a for loop with else

You can also use a for loop with an else clause to iterate through the lists. The else clause will be executed after the loop finishes, unless it is interrupted by a break statement. This method allows you to avoid creating a separate flag variable to track whether the loop completed or was interrupted.

Here is an example of how to use a for loop with else to iterate through list1 and list2:

```list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]

for i in range(min(len(list1), len(list2))):
x = list1[i]
y = list2[i]
print(x + y)
else:
print("Done!")
```
```6
8
10
12
Done!
```

## Method 6: Using list comprehension

Finally, you can use list comprehension to create a new list that combines the elements of the two lists in parallel. This method is concise and efficient, but it creates a new list and may not be suitable for large lists or for cases where you need to perform an operation on each pair of elements without creating a new list.

Here is an example of how to use list comprehension to iterate through list1 and list2 and create a new list with the sums of each pair of elements:

```list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]

result = [x + y for x, y in zip(list1, list2)]
print(result)
```
```[6, 8, 10, 12]
```