w3resource

Python: Sum a specific column of a list in a given list of lists


Sum of Specific Column in List of Lists

Write a Python program to sum a specific column of a list in a given list of lists.

Sample Solution:

Python Code:

# Define a function 'sum_column' that calculates the sum of a specified column in a list of lists
def sum_column(nums, C):
    # Calculate the sum of the specified column 'C' using a generator expression
    result = sum(row[C] for row in nums)
    return result

# Create a list of lists 'nums' representing a 2D matrix
nums = [
        [1, 2, 3, 2],
        [4, 5, 6, 2],
        [7, 8, 9, 5],
    ]

# Print a message indicating the original list of lists
print("Original list of lists:")
# Print the contents of 'nums'
print(nums)

# Set the column index to 0
column = 0
# Print a message indicating the operation to sum the 1st column
print("\nSum: 1st column of the said list of lists:")
# Call the 'sum_column' function with 'nums' and column, then print the result
print(sum_column(nums, column))

# Set the column index to 1
column = 1
# Print a message indicating the operation to sum the 2nd column
print("\nSum: 2nd column of the said list of lists:")
# Call the 'sum_column' function with 'nums' and column, then print the result
print(sum_column(nums, column))

# Set the column index to 3
column = 3
# Print a message indicating the operation to sum the 4th column
print("\nSum: 4th column of the said list of lists:")
# Call the 'sum_column' function with 'nums' and column, then print the result
print(sum_column(nums, column)) 

Sample Output:

Original list of lists:
[[1, 2, 3, 2], [4, 5, 6, 2], [7, 8, 9, 5]]

Sum: 1st column of the said list of lists:
12

Sum: 2nd column of the said list of lists:
15

Sum: 4th column of the said list of lists:
9

Flowchart:

Flowchart: Sum a specific column of a list in a given list of lists.

Python Code Editor:

Previous: Write a Python program to remove empty lists from a given list of lists.
Next: Write a Python program to get the frequency of the elements in a given list of lists.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.