On 06/10/2010 10:47 PM, Anthony Papillion wrote: > Someone helped me with some code yesterday and I'm trying to > understand it. The way they wrote it was > > subjects = (info[2] for info in items)
This is a generator expression, and it creates a generator object. If you loop over it (subjects), you will get all the subjects (or whatever info[2] is), one by one. > > Perhaps I'm not truly understanding what this does. Does this do > anything different than if I wrote > > for info[2] in items > subject = info[2] These two snippets do the same thing: # No. 1. subjects = (info[2] for info in items) for subject in subjects: print (subject) # No. 2. for info in items: subject = info[2] print (subject) However, you could also pass the subjects variable to another function and have it iterate over them - that's something that the simple loop can't do just like that. > > Thanks! > Anthony -- http://mail.python.org/mailman/listinfo/python-list