A question on the creation of list of lists

Chris Kaynor ckaynor at zindagigames.com
Wed Apr 22 18:33:46 CEST 2015

On Wed, Apr 22, 2015 at 9:18 AM, <subhabrata.banerji at gmail.com> wrote:

> Dear Group,
> I am trying to open a bunch of files from a directory and trying to put
> the results in list of lists that is to say,
> that is to say,
> I have a list of file names of a directory, I want to read each one of
> them.
> After reading each one of them, I want to put the results of each file in
> a list.
> These lists would again be inserted to create a list of lists.
> to do this I am trying to write it as follows:
> list_of_files = glob.glob('C:\Python27\*.*')
> print list_of_files
> list1=[]
> list2=[]
> list_N=[list1,list2]
> for i,j in zip(list_of_files,list_N):
>     print i,j
>     x1=open(i,"r").read()
>     x2=j.append(x1)
> all_sent=list_N
> print all_sent

In the original e-mail, it looked like you had an indentation error, which
I think I corrected correctly, though you may have been missing a line. A
few comments with your code:
First, as you only create two lists, it would only ever iterate twice, and
would ignore the rest of the files.
Secondly, you only ever append a single item to the lists.
Third, list.append always returns None, so the line "x2 = j.append(x1)"
will always result in x2 being None. Not that it matters much as you never
use x2 in the code.

Presuming that you actually are doing something in your real code that
would be producing lists for each file dynamically, rather than just the
string contents of the file, you probably should create the child lists in
the loop, rather than outside. Something like:

results = [] # Create the results list for all files.
for path in glob.glob('C:\Python27\*.*'):
    fileResult = [] # Create the result for the file.
    with open(path, 'r') as myFile: # Note that this helps ensure the file
is closed, even in more complicated cases, and in all implementations. It
is not needed in such a simple case for CPython only, however.
        fileResult.append(myFile.read()) # Or whatever real processing you
want to do. file.read() returns a single string, and fileResult in this
example only ever has a single item, so it is a somewhat useless layer of
    results.append(fileResult) # Add the file's result into the full list
of results.

Note that, depending on the processing you are doing, and on what files,
you may want to avoid reading all files into memory at once, and instead
process them incrementally, preferably avoiding even reading all of a
single file. This is particularly easy if processing the file by line.

> Am I doing anything wrong? If any one may kindly suggest?
> Is there any smarter way to do it? I am using Python2.7+
> on MS-Windows 7 Professional Edition.
> Apology for any indentation error.
> Regards,
> Subhabrata Banerjee.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150422/2e5298f7/attachment.html>

More information about the Python-list mailing list