Python – Average String length in list


The Python language is composed of various data structures and one among them is the List data structure which can store elements of different data types within these square brackets. The length of each word or character is added to the overall length of the list. The length of each string of the list can be initialized with a combination of integers, float numbers, and strings. After that, each character or number given is added together and later divided by the length of the list. The methods to get the average string length to follow certain functions.

Average String Length in String

The methods used to find the average string length are using the mean() function, map() function from the statistics library, for loop, and sum() function.

Approach

Approach 1 − Using mean() function

Approach 2 − Using for loop

Approach 3 − Using map() and sum() function

Approach 1: Python program to find the average string length using mean() function from statistics module

The library is used to add the reduce() function in functools module. The strings within the list are initialized as list1. The length of each string of the list is calculated using the lambda function that adds to the accumulator variable. This accumulator variable is initially set as 0. The average is printed in the form of decimal numbers.

Algorithm

  • Step 1 − The functools library is imported to use specific functions for calculating the average value.

  • Step 2 − The list1 is initialized with the string and integer values.

  • Step 3 − The reduce function is mostly used to reduce the elements with the help of an anonymous function called lambda function.

  • Step 4 − At last, the list value is printed.

Example

#import the functools library
from functools import reduce

#initializing the list1 with integers and letters as strings within quotes 
list1 = ["Sharpener", "book", "notepad", "pencil"]

#calculating the length of each word of the string using the reduce function to get the average string length 
list_len = reduce(lambda m, n: m + len(n), list1, 0)

#to get the average of the string length by dividing the list_len by the length of the list1
avg_len = list_len / len(list1)

#printing the string length 
print("Average string length:", avg_len)

Output

Average string length: 6.5

Approach 2: Python program to find the average string length using for loop

The strings within the list are initialized as list1. To get the average value, the lengths of the strings are added, and divided by the number of strings. The respective output is returned or printed.

Algorithm

  • Step 1 − The list1 is initialized with the string values and an empty string is declared.

  • Step 2 − The looping statement is used for iterating through the list data structure.

  • Step 3 − len function calculates the length based on the characters given in the list.

  • Step 4 − As the formula for finding the average can be implemented to get the result.

Example

#initializes the input 
list1 = ["Sharpener", "book", "notepad", "pencil"]
#declaring the list as empty
list_len = 0
for charac in list1:
	list_len += len(charac)
#mean value is calculated by dividing the list by the length
avg_len = list_len/len(list1)
#returns the result
print(" List mean can be calculated as: ", avg_len)

Output

List mean can be calculated as: 6.5

Approach 3: Python Program to calculate the average string length of the list using map method

The list is declared to hold the values of different types. One of the most frequently used functions is a map to match a string of parameters in the given list.

Algorithm

  • Step 1 − The input can be declared with any elements as the user desire.

  • Step 2 − The elements are added to the list and divided by the length of the list.

  • Step 3 − So finally it returns the result.

Example

#declaring the input
num = ["book", "notepad", "pencil"] 
#map function to find the average 
len1 = sum(map(len, num))
#To get the average length of the string
avglen = len1/len(num)
#to print the result
print("Average string length : ", avglen)

Output

Average string length: 5.666666666666667

Conclusion

The Python program is primarily used in dealing with the data like the list is initialized to hold the values like strings, integers, and even float numbers. Like the mathematical calculations involve a simple method to get the average of numbers and the approaches provided above return the average string length.

Updated on: 29-Aug-2023

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements