Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Iterators: What does iglob()'s Iterator provide over glob()'s list?

Given the piece of code:

from glob import glob, iglob  for fn in glob('/*'):     print fn  print ''  for fn in iglob('/*'):     print fn 

Reading the documentation for glob I see that glob() returns a basic list of files and iglob an Iterator. However I'm able to iterate over both and the same list of files is returned by each of them.

I've read the documentation on Iterator but it hasn't shed anymore light on the subject really!

So what benefit does iglob() returning an Iterator provide me over the list from glob()? Do I gain extra functionality over my old friend the lowly list?

like image 693
ghickman Avatar asked Nov 26 '10 16:11

ghickman


People also ask

What is Iglob in Python?

Using Glob() function to find files recursively We can use the function glob. glob() or glob. iglob() directly from glob module to retrieve paths recursively from inside the directories/files and subdirectories/subfiles. Syntax: glob.glob(pathname, *, recursive=False) glob.iglob(pathname, *, recursive=False)

What does glob Iglob return?

The glob API iglob(pathname, recursive=False) Same as glob. glob , except that it returns an iterator, meaning that not all values get stored in memory - so can be much more efficient. glob. escape(pathname) Escapes special characters in the passed in “pathname”.

What is the use of iterator in python?

An iterator is an object that contains a countable number of values. An iterator is an object that can be iterated upon, meaning that you can traverse through all the values. Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__() .

What is __ ITER __ in Python?

The __iter__() method returns the iterator object itself. If required, some initialization can be performed. The __next__() method must return the next item in the sequence. On reaching the end, and in subsequent calls, it must raise StopIteration .


2 Answers

The difference is mentioned in the documentation itself:

Return an iterator which yields the same values as glob() without actually storing them all simultaneously.

Basically list will have all the items in memory. Iterator need not, and hence it requires less memory.

like image 69
amit Avatar answered Sep 26 '22 14:09

amit


Adding to amit's answer. iglob() is useful in the particular case where if you delete a directory in the list, the files and folders in the list will be stored by glob() and hence further access in the loop throws exception. But by using iglob() we can overcome the concurrent modification exception

like image 31
Ridhuvarshan Avatar answered Sep 24 '22 14:09

Ridhuvarshan