What's the difference?

Ethan Furman ethan at stoneleaf.us
Thu Jun 10 17:34:23 EDT 2010


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)
> 
> 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]

Close -- the correct form is

for info in items:
     subject = info[2]

Basically, python goes through the elements of items, assigning each one 
to the name 'info' during that loop; then in the body of the loop, you 
can access the attributes/elements/methods/whatever of the info object.

Hope this helps!

~Ethan~



More information about the Python-list mailing list