Python - Average digits count in a List
Last Updated :
19 Mar, 2023
Given a list of elements extract the average digit count in List.
Input : test_list = [34, 2345, 23, 456, 2, 23, 456787]
Output : 2.857142857142857
Explanation : Average of all digit count. [2+4+2+3+1+2+6 = 20, 20/7 = 2.857142857142857]
Input : test_list = [34, 1, 456]
Output : 2.0
Explanation : Average of all digit count. [1 + 2 + 3 = 6, 6 / 3 = 2]
Method #1 : Using len() + loop + str()
In this, we iterate each element, convert to string, and find its length, perform summation using counter, and then divide the result with the total element in the list to get results.
Python3
# Python3 code to demonstrate working of
# Average digits count
# Using len() + loop + str()
# initializing list
test_list = [34, 2345, 23, 456, 2, 23, 456787]
# printing original list
print("The original list is : " + str(test_list))
sumd = 0
for ele in test_list:
# summing digits length
sumd += len(str(ele))
# getting result after dividing total digits by elements
res = sumd / len(test_list)
# printing result
print("Average digits length : " + str(res))
OutputThe original list is : [34, 2345, 23, 456, 2, 23, 456787]
Average digits length : 2.857142857142857
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using len() + sum() + str()
In this, the task of getting a sum is done using sum(), extends a compact way to solve the problem.
Python3
# Python3 code to demonstrate working of
# Average digits count
# Using len() + sum() + str()
# initializing list
test_list = [34, 2345, 23, 456, 2, 23, 456787]
# printing original list
print("The original list is : " + str(test_list))
# getting summation and dividing by total length
res = sum([len(str(ele)) for ele in test_list]) / len(test_list)
# printing result
print("Average digits length " + str(res))
OutputThe original list is : [34, 2345, 23, 456, 2, 23, 456787]
Average digits length 2.857142857142857
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.
Method #3 : Using the mean() function from the statistics module :
Algorithm:
1.Initialize a list to hold the lengths of digits for each element in the input list.
2.Iterate over the elements in the input list and convert each element to string and find the length of the string. Append this length to the list from step 1.
3.Calculate the average of the list from step 1 using the mean() function from the statistics module.
4.Return the average digits count.
Python3
# import the statistics module
import statistics
# initializing list
test_list = [34, 2345, 23, 456, 2, 23, 456787]
# printing original list
print("The original list is : " + str(test_list))
# calculate the average digits count using the mean() function
digits_lengths = [len(str(num)) for num in test_list]
average_digits = statistics.mean(digits_lengths)
# print the result
print("Average digits count:", average_digits)
#This code is contributed by Jyothi pinjala.
OutputThe original list is : [34, 2345, 23, 456, 2, 23, 456787]
Average digits count: 2.857142857142857
Time Complexity: O(n), where n is the length of the input list. The for loop that iterates over the elements in the input list has a time complexity of O(n) and finding the length of a string has a time complexity of O(1).
Auxiliary Space: O(n), where n is the length of the input list. We need to store the length of each element's digits in a separate list, which has a space complexity of O(n).
Method #4: Using list comprehension and the sum() function:
Initialize the list of integers.
Use list comprehension to get the length of each integer's digits and create a new list of those lengths.
Use the sum() function to get the total length of all the digits.
Divide the total length by the number of integers in the list to get the average length.
Print the result.
Python3
# Python3 code to demonstrate working of
# Average digits count
# Using list comprehension and sum()
# initializing list
test_list = [34, 2345, 23, 456, 2, 23, 456787]
# printing original list
print("The original list is : " + str(test_list))
# using list comprehension to get lengths of digits
lengths = [len(str(ele)) for ele in test_list]
# summing the lengths to get total length
total_length = sum(lengths)
# calculating average
avg_length = total_length / len(test_list)
# printing result
print("Average digits length : " + str(avg_length))
OutputThe original list is : [34, 2345, 23, 456, 2, 23, 456787]
Average digits length : 2.857142857142857
Time complexity: O(n), where n is the length of the input list
Auxiliary space: O(n), where n is the length of the input list
Similar Reads
Column Average in Record List - Python Given a list of records where each record contains multiple fields, the task is to compute the average of a specific column. Each record is represented as a dictionary or a list, and the goal is to extract values from the chosen column and calculate their average. Letâs explore different methods to
3 min read
Find Sum and Average of List in Python Our goal is to find sum and average of List in Python. The simplest way to do is by using a built-in method sum() and len(). For example, list of numbers is, [10, 20, 30, 40, 50] the sum is the total of all these numbers and the average is the sum divided by the number of elements in the list.Using
2 min read
Python - Rear elements Average in List Sometimes, while working with data, we can have a problem in which we need to perform the mean of all the rear elements that come after K. This can be an application in Mathematics and Data Science domain. Let us discuss certain ways in which this task can be performed. Method #1 : Using sum() + li
6 min read
Python | Record elements Average in List Given a list of tuples, write a program to find average of similar tuples in list. Examples: Input: [('Geeks', 10), ('For', 10), ('Geeks', 2), ('For', 9), ('Geeks', 10)] Output: Resultant list of tuples: [('For', 9.5), ('Geeks', 7.333333333333333)] Input: [('Akshat', 10), ('Garg', 10), ('Akshat', 2)
3 min read
Python | Average String length in list Sometimes, while working with data, we can have a problem in which we need to gather information of average length of String data in list. This kind of information might be useful in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehen
8 min read
Python - Sort by a particular digit count in elements Given a list of elements, sort by K digit in each element. Examples: Input : test_list = [4322, 2122, 123, 1344], K = 2 Output : [1344, 123, 4322, 2122] Explanation : 0 < 1 < 2 < 3, sorted by count of 2 in each element. Input : test_list = [4322, 2122, 1344], K = 2 Output : [1344, 4322, 212
5 min read