# Python | Average String length in list

• Last Updated : 21 Jan, 2023

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 comprehension + sum() + len() The combination of above functions can be used to perform this task. In this we compute lengths of all Strings using list comprehension and then divide the sum by length of list using len() and sum().

## Python3

 `# Python3 code to demonstrate working of``# Average String lengths in list``# using list comprehension + sum() + len()` `# initialize list``test_list ``=` `[``'gfg'``, ``'is'``, ``'best'``, ``'for'``, ``'geeks'``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# Average String lengths in list``# using list comprehension + sum() + len()``temp ``=` `[``len``(ele) ``for` `ele ``in` `test_list]``res ``=` `0` `if` `len``(temp) ``=``=` `0` `else` `(``float``(``sum``(temp)) ``/` `len``(temp))` `# printing result``print``(``"The Average length of String in list is : "` `+` `str``(res))`

Output :

```The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The Average length of String in list is : 3.4```

Method #2 : Using map() + sum() + len() The combination of above functions can also be used to perform this task. In this, we compute lengths using map(). Rest all the logic is similar to above method.

## Python3

 `# Python3 code to demonstrate working of``# Average String lengths in list``# using map() + sum() + len()` `# initialize list``test_list ``=` `[``'gfg'``, ``'is'``, ``'best'``, ``'for'``, ``'geeks'``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# Average String lengths in list``# using map() + sum() + len()``res ``=` `sum``(``map``(``len``, test_list))``/``float``(``len``(test_list))` `# printing result``print``(``"The Average length of String in list is : "` `+` `str``(res))`

Output :

```The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The Average length of String in list is : 3.4```

Method #3 : Using len() and mean() method of statistics module

## Python3

 `# Python3 code to demonstrate working of``# Average String lengths in list``import` `statistics``# initialize list``test_list ``=` `[``'gfg'``, ``'is'``, ``'best'``, ``'for'``, ``'geeks'``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# Average String lengths in list``x``=``[]``for` `i ``in` `test_list:``    ``x.append(``len``(i))``res``=``statistics.mean(x)``# printing result``print``(``"The Average length of String in list is : "` `+` `str``(res))`

Output

```The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The Average length of String in list is : 3.4```

Method #4 : Using reduce()

## Python3

 `from` `functools ``import` `reduce` `# Python3 code to demonstrate working of``# Average String lengths in list``# using reduce()`` ` `# initialize list``test_list ``=` `[``'gfg'``, ``'is'``, ``'best'``, ``'for'``, ``'geeks'``]`` ` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))`` ` `# Average String lengths in list``# using reduce()``res ``=` `reduce``(``lambda` `x, y: x ``+` `y, ``map``(``len``, test_list))``/``len``(test_list)`` ` `# printing result``print``(``"The Average length of String in list is : "` `+` `str``(res))` `#this code is contributed by edula vinay kumar reddy`

Output

```The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The Average length of String in list is : 3.4```

In this method, we first use map() to calculate the length of each string in the list. Then, we use reduce() function to get the sum of all lengths. Finally, we divide the sum by the length of the list to get the average length.

Time Complexity: O(n)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up