[Tutor] Enumerate vs DictReader object manipulation:

Ben Finney ben+python at benfinney.id.au
Wed Feb 3 16:24:59 EST 2016

Ek Esawi <esawiek at gmail.com> writes:

> I have a code that reads a csv file via DictReader. I ran into a peculiar
> problem. The python interpreter ignores the 2nd code. That is if I put the
> reader iterator 1st, like the code below, the enumerate code is ignored; if
> I put the enumerate code 1st, the reader code is ignored.

You have discovered the difference between an iterable (an object you
can iterate over with ‘for’), versus a sequence (an object whose items
remain in place and can be iterated many times).

Every sequence is an iterable, but not vice versa.

File objects are iterables, but not sequences. Each time you ask for the
next item, you can't ask the file object for that item again; it is
“consumed” by the act of iteration.

 \       “When I was little, my grandfather used to make me stand in a |
  `\   closet for five minutes without moving. He said it was elevator |
_o__)                                        practice.” —Steven Wright |
Ben Finney

More information about the Tutor mailing list