Add list elements with a multi-list based on index in Python


Lists can be nested. Which means we have smaller lists as elements inside a bigger list. In this article we solve the challenge of adding the elements of a simple list to the elements of a nested list. If the length of the lists are different then the length of the smaller list becomes the maximum length of the resulting list.

Below are the various methods to accomplish this.

Using for Loop

In this method, we take the length of the smaller list and loop through the elements of this list adding it to the elements of the bigger list. Here we use the append function to append each element into the result list.

Example

 Live Demo

simple_list = [25, 35, 45, 55, 65]
nested_list = [[5,10], [10], [5,15], [25], [5,10,15],[5,6]]
result_list = []

for n in range(len(simple_list)):
   var = []
   for m in nested_list[n]:
      var.append(m + simple_list[n])
   result_list.append(var)
print("The first list :", simple_list)
print("The nested list :", nested_list)
print("Result :",result_list)

Output

Running the above code gives us the following result −

The first list : [25, 35, 45, 55, 65]
The nested list : [[5, 10], [10], [5, 15], [25], [5, 10, 15], [5, 6]]
Result : [[30, 35], [45], [50, 60], [80], [70, 75, 80]]

Using enumerate()

The enumerate() function takes a collection like list or tuple and returns it as an enumerate object. In this approach we first create an outer for loop having the enumerate function to get each element of the nested list and then add them to the respective elements in the simple list through an inner for loop.

Example

 Live Demo

simple_list = [25, 35, 45, 55, 65,25]
nested_list = [[5,10], [10], [5,15], [25], [5,10,15]]
result_list = []

# using enumerate
result_list = [[val + simple_list[p] for val in q] for p, q in enumerate(nested_list)]
print("The first list :", simple_list)
print("The nested list :", nested_list)
print("Result :",result_list)

Output

Running the above code gives us the following result −

The first list : [25, 35, 45, 55, 65, 25]
The nested list : [[5, 10], [10], [5, 15], [25], [5, 10, 15]]
Result : [[30, 35], [45], [50, 60], [80], [70, 75, 80]]

Using zip()

In this approach we repeat the above program but using zip () instead of enumerate. The zip() takes in both the lists as its input.

Example

 Live Demo

simple_list = [25, 35, 45, 55, 65,25]
nested_list = [[5,10], [10], [5,15], [25], [5,10,15]]
result_list = []

result_list = [[w + u for w in v ]for u, v in zip(simple_list, nested_list)]

print("The first list :", simple_list)
print("The nested list :", nested_list)
print("Result :",result_list)

Output

Running the above code gives us the following result −

The first list : [25, 35, 45, 55, 65, 25]
The nested list : [[5, 10], [10], [5, 15], [25], [5, 10, 15]]
Result : [[30, 35], [45], [50, 60], [80], [70, 75, 80]]

Updated on: 18-Feb-2020

467 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements