Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How does this implementation of the Python sum function work?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading