Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Python – Average digits count in a List
When it is required to count average digits in a list, a simple iteration, the ‘str’ method and the ‘/’ operator is used.
Below is a demonstration of the same −
Example
my_list = [324, 5345, 243, 746, 432, 463, 946787]
print("The list is :")
print(my_list)
sum_digits = 0
for ele in my_list:
sum_digits += len(str(ele))
my_result = sum_digits / len(my_list)
print("The result is :")
print(my_result)
Output
The list is : [324, 5345, 243, 746, 432, 463, 946787] The result is : 3.5714285714285716
Explanation
A list is defined and displayed on the console.
A variable is initialized to 0.
The list is iterated over, and the sum of the digits is calculated by first converting the element to a list and determining its length using ‘len’ method.
The average of these digits is calculated.
This result is assigned to a variable.
This is the output that is displayed on the console.
Advertisements
