Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Add list elements with a multi-list based on index in Python
When working with nested lists, you may need to add elements from a simple list to elements within a nested list based on their index positions. This operation pairs each element from the simple list with the corresponding sublist in the nested list and adds the simple list element to each item in that sublist.
If the lists have different lengths, the operation is limited by the shorter list. Below are three efficient methods to accomplish this task.
Using for Loop
This method uses nested loops to iterate through both lists simultaneously. We take the length of the shorter list and add each simple list element to all elements in the corresponding nested sublist ?
Example
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)
The output of the above code is ?
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() with List Comprehension
The enumerate() function returns both the index and value of each element, making it perfect for this task. Combined with list comprehension, this creates a more concise solution ?
Example
simple_list = [25, 35, 45, 55, 65, 25]
nested_list = [[5, 10], [10], [5, 15], [25], [5, 10, 15]]
# Using enumerate with list comprehension
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)
The output of the above code is ?
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() Function
The zip() function pairs elements from both lists simultaneously, stopping when the shorter list is exhausted. This approach is clean and handles different list lengths automatically ?
Example
simple_list = [25, 35, 45, 55, 65, 25]
nested_list = [[5, 10], [10], [5, 15], [25], [5, 10, 15]]
# Using zip with list comprehension
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)
The output of the above code is ?
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]]
Comparison of Methods
| Method | Readability | Performance | Best For |
|---|---|---|---|
| for Loop | High | Good | Beginners, complex logic |
| enumerate() | Medium | Better | When you need index access |
| zip() | High | Best | Clean, Pythonic solutions |
Conclusion
Use zip() for the most Pythonic and efficient solution. Use enumerate() when you need explicit index control. Use traditional loops when logic becomes complex or for educational purposes.
