I want to calculate the average value of several lists in python. These lists contain numbers as strings. Empty string isn\'t zero, it means a missing value.
The be
You can discard the square brackets. sum
accepts generator expressions, too:
total = sum(int(n) if n else 0 for n in num)
length = sum(1 if n else 0 for n in num)
And since generators yields the value only when needed, you save the expensive cost of storing a list in the memory. Especially if you're dealing with bigger datas.