Open In App

Python | Iterating two lists at once

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python list, we can have a problem in which we have to iterate over two list elements. Iterating one after another is an option, but it’s more cumbersome and a one-two liner is always recommended over that. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using loop + “+” operator The combination of above functionalities can make our task easier. But the drawback here is that we might have to concatenate the list and hence would consume more memory than desired. 

Python3




# Python3 code to demonstrate working of
# Iterating two lists at once
# using loop + "+" operator
 
# initializing lists
test_list1 = [4, 5, 3, 6, 2]
test_list2 = [7, 9, 10, 0]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Iterating two lists at once
# using loop + "+" operator
# printing result
print("The paired list contents are : ")
for ele in test_list1 + test_list2:
    print(ele, end =" ")


Output : 

The original list 1 is : [4, 5, 3, 6, 2]
The original list 2 is : [7, 9, 10, 0]
The paired list contents are : 
4 5 3 6 2 7 9 10 0 

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.  
Auxiliary Space: O(n), where n is the number of elements in the new paired list 

  Method #2 : Using chain() This is the method similar to above one, but it’s slightly more memory efficient as the chain() is used to perform the task and creates an iterator internally. 

Python3




# Python3 code to demonstrate working of
# Iterating two lists at once
# using chain()
from itertools import chain
 
# initializing lists
test_list1 = [4, 5, 3, 6, 2]
test_list2 = [7, 9, 10, 0]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Iterating two lists at once
# using chain()
# printing result
print("The paired list contents are : ")
for ele in chain(test_list1, test_list2):
    print(ele, end =" ")


Output : 

The original list 1 is : [4, 5, 3, 6, 2]
The original list 2 is : [7, 9, 10, 0]
The paired list contents are : 
4 5 3 6 2 7 9 10 0 

The time complexity of the given code is O(n+m), where n and m are the lengths of test_list1 and test_list2.

The space complexity of the code is O(n+m) as well, because it creates an iterator using the chain() function, which generates a new iterable object containing all the elements from both lists. 

 Method #3 : Using append+loop()

Approach

Append each element in original_list2 to the end of original_list1, thus combining the two lists into a single list.

step-by-step algorithm

1. Define original_list1 and original_list2.
2. Loop over each element in original_list2.
3. For each element, append it to the end of original_list1 using the append() method.
4. After all elements in original_list2 have been appended to original_list1

5. print the contents of original_list1 using a loop.

Python3




# Define the two original lists
original_list1 = [4, 5, 3, 6, 2]
original_list2 = [7, 9, 10, 0]
 
# Loop over each element in original_list2
for i in original_list2:
    # Append the element to original_list1
    original_list1.append(i)
 
# Print the contents of the modified original_list1
print("The paired list contents are : ")
for element in original_list1:
    print(element, end=" ")


Output

The paired list contents are : 
4 5 3 6 2 7 9 10 0 

Time complexity: O(n+m), where n is the length of original_list1 and m is the length of original_list2. This is because we are iterating over all the elements of original_list2 and appending each of them to original_list1.

Auxiliary Space: O(n+m), as we are modifying the original_list1 list in-place by appending elements from original_list2. Therefore, the size of the resulting list is the sum of the sizes of the two original lists.

Method#4: Using Recursive method.

1. Define a recursive function `concatenate_lists(list1, list2, index)` that takes in two lists `list1` and `list2`, and an `index` that keeps track of the current position in `list2`.
2. If `index` is equal to the length of `list2`, return `list1` as the concatenation is complete.
3. Otherwise, append the element at index `index` of `list2` to `list1`.
4. Increment the index by 1 and recursively call `concatenate_lists` with the updated arguments `list1`, `list2`, and `index`.
5. Return the result of the recursive call.

Python3




def combine_lists_recursive(list1, list2):
    # Base case: if either list is empty, return the other list
    if len(list1) == 0:
        return list2
    elif len(list2) == 0:
        return list1
     
    # Recursive case: append the first element of list2 to list1, and recurse on the rest
    list1.append(list2[0])
    return combine_lists_recursive(list1, list2[1:])
original_list1 = [4, 5, 3, 6, 2]
original_list2 = [7, 9, 10, 0]
combined_list = combine_lists_recursive(original_list1, original_list2)
print("The paired list contents are : ",combined_list)


Output

The paired list contents are :  [4, 5, 3, 6, 2, 7, 9, 10, 0]

Time complexity: O(n), where n is the total number of elements in the concatenated lists, as each element needs to be accessed once.

Space complexity: O(n), as the function call stack grows with each recursive call and the resulting concatenated list contains all the elements from the original lists.



Last Updated : 20 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads