Python Program to Calculate the Average of a Number’s digits of Every Number in Given List

Python Program to Calculate the Average of a Number’s digits of Every Number in Given List

Given a list, the task is to calculate the average of every number digit in the given List in Python.

Examples:

Example1:

Input:

Given list =[9, 15, 21, 356, 3243, 9139, 4467285, 123456, 5783892, 4535363, 69]

Output:

The list before calculating Average of digits =  [9, 15, 21, 356, 3243, 9139, 4467285, 123456, 5783892, 4535363, 69]
The list after calculating Average of digits =  [9.0, 3.0, 1.5, 4.666666666666667, 3.0, 5.5, 5.142857142857143, 3.5, 6.0, 4.142857142857

Example2:

Input:

Given list = [245, 1739, 102, 199, 488, 58]

Output:

The list before calculating Average of digits = [245, 1739, 102, 199, 488, 58]
The list after calculating Average of digits = [3.6666666666666665, 5.0, 1.0, 6.333333333333333, 6.666666666666667, 6.5]

Program to Calculate the Average of a Number’s digits of Every Number in Given List in Python

Below are the ways to Calculate the Average of a Number’s digits of Every Number in the given List in Python

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Take an empty to store the Average digit sums of all the individual elements using [] or list() say resulist.
  • Create a function numAverageDigits() which accepts the number as an argument and returns the Average of all the digits in the given number.
  • Traverse the given list using For loop.
  • Pass the iterator value(element) to the numAverageDigits() as an argument and append the result to the resulist using the append() function.
  • Print the resulist.
  • The Exit of the Program.

Below is the implementation:

# Create a function numAverageDigits() which accepts the number as an argument and
# returns the Average of all the digits in the given Number.


def numAverageDigits(numbe):
  # convert the given number to string using str() function
    stringnum = str(numbe)
    # Create a list of digits using map(),list(),int functions
    digitslist = list(map(int, stringnum))
    # calculate the sum of digits of the above digitslist
    sumdigits = sum(digitslist)
    # calculate the total number of digits by calculating length of the digits list
    numOfDigits = len(digitslist)
    # calculate the average of digits of the given number
    # by diving sum of digits by number of digits
    avgdigits = sumdigits/numOfDigits
    # Return the average of the digits.
    return avgdigits


# Give the list as static input and store it in a variable.
givenlist = [9, 15, 21, 356, 3243, 9139, 4467285, 123456, 5783892, 4535363, 69]
# Take an empty to store the Average digit sums of all the individual elements
# using [] or list() say resulist.
resulist = []
# Traverse the given list using For loop.
for Number in givenlist:
    # Pass the iterator value(element) to the numAverageDigits() as an argument
    # and append the result to the resulist using the append() function.
    resulist.append(numAverageDigits(Number))
# Print the givenlist before calculating Average of digits
print('The list before calculating Average of digits = ', givenlist)
# Print the resulist after calculating Average of digits
print('The list after calculating Average of digits = ', resulist)

Output:

The list before calculating Average of digits =  [9, 15, 21, 356, 3243, 9139, 4467285, 123456, 5783892, 4535363, 69]
The list after calculating Average of digits =  [9.0, 3.0, 1.5, 4.666666666666667, 3.0, 5.5, 5.142857142857143, 3.5, 6.0, 4.142857142857143, 7.5]

Method #2: Using For Loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Take an empty to store the Average digit sums of all the individual elements using [] or list() say resulist.
  • Create a function numAverageDigits() which accepts the number as an argument and returns the Average of all the digits in the given number.
  • Traverse the given list using For loop.
  • Pass the iterator value(element) to the numAverageDigits() as an argument and append the result to the resulist using the append() function.
  • Print the resulist.
  • The Exit of the Program.

Below is the implementation:

# Create a function numAverageDigits() which accepts the number as an argument and
# returns the Average of all the digits in the given Number.


def numAverageDigits(numbe):
  # convert the given number to string using str() function
    stringnum = str(numbe)
    # Create a list of digits using map(),list(),int functions
    digitslist = list(map(int, stringnum))
    # calculate the sum of digits of the above digitslist
    sumdigits = sum(digitslist)
    # calculate the total number of digits by calculating length of the digits list
    numOfDigits = len(digitslist)
    # calculate the average of digits of the given number
    # by diving sum of digits by number of digits
    avgdigits = sumdigits/numOfDigits
    # Return the average of the digits.
    return avgdigits


# Give the list as user input using list(),map(),input(),and split() functions.
# store it in a variable.
givenlist = list(
    map(int, input('Enter some random List Elements separated by spaces = ').split()))
# Take an empty to store the Average digit sums of all the individual elements
# using [] or list() say resulist.
resulist = []
# Traverse the given list using For loop.
for Number in givenlist:
    # Pass the iterator value(element) to the numAverageDigits() as an argument
    # and append the result to the resulist using the append() function.
    resulist.append(numAverageDigits(Number))
# Print the givenlist before calculating Average of digits
print('The list before calculating Average of digits = ', givenlist)
# Print the resulist after calculating Average of digits
print('The list after calculating Average of digits = ', resulist)

Output:

Enter some random List Elements separated by spaces = 245 1739 102 199 488 58
The list before calculating Average of digits = [245, 1739, 102, 199, 488, 58]
The list after calculating Average of digits = [3.6666666666666665, 5.0, 1.0, 6.333333333333333, 6.666666666666667, 6.5]

Related Programs: