What's the difference?
Ethan Furman
ethan at stoneleaf.us
Thu Jun 10 19:51:37 EDT 2010
Ethan Furman wrote:
> 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~
Ack. My correction only dealt with the info[2] issue on the for line;
Emille's answer properly matches the original code.
Sorry.
~Ethan~
More information about the Python-list
mailing list