Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does readlines() return a list or an iterator in Python 3?

I've read in "Dive into Python 3" that:

"The readlines() method now returns an iterator, so it is just as efficient as xreadlines() was in Python 2".

See: Appendix A: Porting Code to Python 3 with 2to3: A.26 xreadlines() I/O method.

I'm not sure that's true because they don't mention it here: http://docs.python.org/release/3.0.1/whatsnew/3.0.html . How can I check that?

like image 558
snakile Avatar asked Aug 22 '10 10:08

snakile


People also ask

Does Readlines return a list Python?

The readlines() method returns a list containing each line in the file as a list item. Use the hint parameter to limit the number of lines returned. If the total number of bytes returned exceeds the specified number, no more lines are returned.

What does the Readlines method returns in Python?

The readlines method returns the entire contents of the entire file as a list of strings, where each item in the list is one line of the file. The readline method reads one line from the file and returns it as a string.

What do readline () and Readlines () return respectively?

Python readline() method will return a line from the file when called. readlines() method will return all the lines in a file in the format of a list where each element is a line in the file.

What is the purpose of the Readlines () function?

readlines() is used to read all the lines at a single go and then return them as each line a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines.


2 Answers

The readlines method doesn't return an iterator in Python 3, it returns a list

Help on built-in function readlines:  readlines(...)     Return a list of lines from the stream. 

To check, just call it from an interactive session - it will return a list, rather than an iterator:

>>> type(f.readlines()) <class 'list'> 

Dive into Python appears to be wrong in this case.


xreadlines has been deprecated since Python 2.3 when file objects became their own iterators. The way to get the same efficiency as xreadlines is instead of using

 for line in f.xreadlines(): 

you should use simply

 for line in f: 

This gets you the iterator that you want, and helps to explain why readlines didn't need to change its behaviour in Python 3 - it can still return a full list, with the line in f idiom giving the iterative approach, and the long-deprecated xreadlines has been removed completely.

like image 189
Scott Griffiths Avatar answered Sep 18 '22 15:09

Scott Griffiths


Like this:

Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> f = open('/junk/so/foo.txt') >>> type(f.readlines()) <class 'list'> >>> help(f.readlines) Help on built-in function readlines:  readlines(...)     Return a list of lines from the stream.      hint can be specified to control the number of lines read: no more     lines will be read if the total size (in bytes/characters) of all     lines so far exceeds hint.  >>> 
like image 41
John Machin Avatar answered Sep 16 '22 15:09

John Machin