w3resource

Python: Sum two or more lists, the lengths of the lists may be different

Python List: Exercise - 194 with Solution

Sum Lists with Different Lengths

Write a Python program to sum two or more lists. The lengths of the lists may be different.

Visual Presentation:

Python List: Sum two or more lists, the lengths of the lists may be different.

Sample Solution:

Python Code:

# Define a function called 'sum_lists_diff_length' that calculates the sum of lists with different lengths.
def sum_lists_diff_length(test_list):
    # Create a result list using a list comprehension.
    # 1. For each sublist 'x' in 'test_list', calculate the sum of its elements.
    # 2. Use 'zip(*map(...))' to ensure that all sublists have the same length by padding shorter sublists with zeros.
    result = [sum(x) for x in zip(*map(lambda x: x + [0] * max(map(len, test_list)) if len(x) < max(map(len, test_list)) else x, test_list))]
    return result  # Return the list of sums.

# Create a list of lists 'nums', where each sublist contains integers.
nums = [[1, 2, 4], [2, 4, 4], [1, 2]]

# Print a message indicating the original list of lists.
print("\nOriginal list:")
# Print the original list 'nums'.
print(nums)

# Print a message indicating the sum of lists with different lengths.
print("Sum said lists with different lengths:")
# Call the 'sum_lists_diff_length' function with 'nums' and print the result (sums of sublists).
print(sum_lists_diff_length(nums))

# Create another list of lists 'nums' with different lengths.
nums = [[1], [2, 4, 4], [1, 2], [4]]

# Print a message indicating the original list of lists.
print("\nOriginal list:")
# Print the original list 'nums'.
print(nums)

# Print a message indicating the sum of lists with different lengths.
print("Sum said lists with different lengths:")
# Call the 'sum_lists_diff_length' function with 'nums' and print the result (sums of sublists).
print(sum_lists_diff_length(nums)) 

Sample Output:

Original list:
[[1, 2, 4], [2, 4, 4], [1, 2]]
Sum said lists with different lengths:
[4, 8, 8]

Original list:
[[1], [2, 4, 4], [1, 2], [4]]
Sum said lists with different lengths:
[8, 6, 4]

Flowchart:

Flowchart: Sum two or more lists, the lengths of the lists may be different.

Python Code Editor:

Previous: Write a Python program to find the dimension of a given matrix.
Next: Write a Python program to traverse a given list in reverse order, also print the elements with original index.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-194.php