[Tutor] how does this list comprehension work?

Christopher Spears cspears2002 at yahoo.com
Wed Mar 8 18:32:43 CET 2006


I copied this program from Learning Python and got it
to work on Windows XP:

import sys, glob
print sys.argv[1:]
sys.argv = [item for arg in sys.argv for item in
glob.glob(arg)]
print sys.argv[1:]

What the program does is first print the glob and then
a list of everything caught by the glob.  For example:

['*.py']
['showglob.py']

The key to the script is the list comprehension, which
I am having trouble dissecting.  How do I go about
trying to figure out how it works?


"I'm the last person to pretend that I'm a radio.  I'd rather go out and be a color television set."
-David Bowie

"Who dares wins"
-British military motto

"I generally know what I'm doing."
-Buster Keaton


More information about the Tutor mailing list