w3resource

Python: Compute average of two given lists

Python List: Exercise - 105 with Solution

Write a Python program to compute average of two given lists.

Sample Solution:

Python Code:

# Define a function 'average_two_lists' that calculates the average of two lists
def average_two_lists(nums1, nums2):
    # Calculate the average by summing the elements of both lists and dividing by their combined length
    result = sum(nums1 + nums2) / len(nums1 + nums2)
    return result

# Create two lists 'nums1' and 'nums2' containing numbers
nums1 = [1, 1, 3, 4, 4, 5, 6, 7]
nums2 = [0, 1, 2, 3, 4, 4, 5, 7, 8]

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

# Print a message indicating that the average of the two lists will be calculated
print("\nAverage of two lists:")

# Call the 'average_two_lists' function with 'nums1' and 'nums2' and print the result
print(average_two_lists(nums1, nums2))

Sample Output:

Original list:
[1, 1, 3, 4, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 4, 5, 7, 8]

Average of two lists:
3.823529411764706

Flowchart:

Flowchart: Compute average of two given lists.

Python Code Editor:

Previous: Write a Python program to find the difference between consecutive numbers in a given list.
Next: Write a Python program to count integer in a given mixed list.

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://w3resource.com/python-exercises/list/python-data-type-list-exercise-105.php