3

I have found an example online of how to count items in a list with the sum() function in Python; however, when I search for how to use the sum() function on the internet, all I can find is the basic sum(iterable, start), which adds numbers together from each element of the list/array.

Code I found, where each line of the file contains one word, and file = open("words.txt", "r"):

wordsInFile = sum(1 for line in file)

this works in my program, and I kind of see what is happening, but I would like to learn more about this kind of syntax, and what it can or can't recognize besides line. It seems pretty efficient, but I can't find any website explaining how it works, which prevents me from using this in the future in other contexts.

Brian61354270
  • 8,690
  • 4
  • 21
  • 43

1 Answers1

4

This expression is a generator.

First, let's write it a bit differently

wordsInFile = sum([1 for line in file])

In this form, [1 for line in file] is called a list comprehension. It's basically a for loop which produces a list, wrapped up into one line. It's similar to

wordsInFile = []
for line in file:
  wordsInFile.append(1)

but a lot more concise.

Now, when we remove the brackets

wordsInFile = sum(1 for line in file)

we get what's called a generator expression. It's basically the same as what I wrote before except that it doesn't produce an intermediate list. It produces a special iterator object that supplies the values on-demand, which is more efficient for a function like sum that only uses its input once (and hence doesn't need us to waste a bunch of memory producing a big list).

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116