[Tutor] Understanding Nested Commands

Alan Colburn aicolburn@yahoo.com
Tue, 27 Aug 2002 15:17:20 -0700


A few weeks ago I asked the list:

"I'd like to save a file, with a list of lists, in a format that could be
read directly into a spreadsheet. Each list would represent a different row,
and each list element a different column. How could I go about doing this?
(List elements are strings.)"

One response suggested this:

>>> myList = [['a','b','c'],['d','e','f'],['g','h','i']]
>>> '\n'.join([','.join(sublist) for sublist in myList])

It's a great solution that does everything I wanted. However, I'm slightly
stumped in understanding how it works. Can someone take me through what's
going
on? Or, alternatively, show me how I might do the exact same thing via
multiple lines, i.e.,

for sublist in myList:
    for member in sublist:
        #etc.

I understand the .join command and everything else that's going on here. I
could, of course, just continue using the statement as is ... I just have
this insatiable desire to know :-)

Thanks, as always -- Al