Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any built-in way to get the length of an iterable in python?

For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines.

One quick way is to do this:

lines = len(list(open(fname))) 

However, this loads the whole file into memory (at once). This rather defeats the purpose of an iterator (which only needs to keep the current line in memory).

This doesn't work:

lines = len(line for line in open(fname)) 

as generators don't have a length.

Is there any way to do this short of defining a count function?

def count(i):     c = 0     for el in i: c += 1     return c 

To clarify, I understand that the whole file will have to be read! I just don't want it in memory all at once

like image 249
Claudiu Avatar asked Dec 24 '08 05:12

Claudiu


People also ask

Can you get the length of an iterable?

To get the length of an iterator in Python:Pass the list to the len() function, e.g. len(list(gen)) .

Do all Iterables have Len?

Python3: no len() for iterators Python 3 has an odd oversight; there's no function for finding the length of an iterable.

What function is used to get the size of elements of an iterable object Python?

Using the len() function is the most common way to get the length of an iterable.

What does ITER function do in Python?

The iter() function returns an iterator object.


2 Answers

Short of iterating through the iterable and counting the number of iterations, no. That's what makes it an iterable and not a list. This isn't really even a python-specific problem. Look at the classic linked-list data structure. Finding the length is an O(n) operation that involves iterating the whole list to find the number of elements.

As mcrute mentioned above, you can probably reduce your function to:

def count_iterable(i):     return sum(1 for e in i) 

Of course, if you're defining your own iterable object you can always implement __len__ yourself and keep an element count somewhere.

like image 194
Kamil Kisiel Avatar answered Oct 18 '22 17:10

Kamil Kisiel


If you need a count of lines you can do this, I don't know of any better way to do it:

line_count = sum(1 for line in open("yourfile.txt")) 
like image 38
mcrute Avatar answered Oct 18 '22 15:10

mcrute