I do not follow why you use i
in i in info
together with count
. If info
is enumerable and enumerating has the same effect as accessing with a zero offset index (like you seem to do with count
, you can rewrite your loop like:
lst = []
for infoi in info:
num = infoi["count"]
# print num
lst.append(num)
print sum(lst)
Now you can convert this to the following list comprehension:
sum([infoi["count"] for infoi in info])
Finally you do not need to materialize the comprehension to a list, sum
can work on a generator:
sum(infoi["count"] for infoi in info)
This can be more efficient, since you will not construct a list first with all the values: sum
will enumerate over all items and thus will result in constant memory usage.