I have a large list containing a lot of data which is read from a csv file. For simplicity I will give you a dummy list which will have far less data in it.
list1 = ['foo', 'bar', 'bob', 'jess', 'google', 'alphabet']
I want to find the average length of strings within the list. I am currently doing it like this:
all_lengths = []
num_of_strings = len(list1)
for item in list1:
string_size = len(item)
all_lengths.append(string_size)
total_size = sum(all_lengths)
ave_size = float(total_size) / float(num_of_strings)
The problem is that because the real list is so large it is taking an incredibly long time to perform this.
Is there a more optimal or elegant way to perform this.
Also, for what it's worth, using Python2.7
total_avg = sum( map(len, strings) ) / len(strings)
The problem in your code is in this line of code :
total_size = sum(all_lengths)
There's no need to calculate this in each loop of the cycle.
Better make this after cycle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With