0

I'm doing some tests with lists, enumerate() method and CSV files.

I'm using the writerows() method to save an enumerate object to a .csv file. All works fine but the list / enumerate object becomes empty after the writing is done.

Why is this happening ? How can I keep the values in my list (do I have to save them in amother variable)?

I'm on Windows 10 using Python 3.6.4

Here is My Code:

import csv

b = [1,2,3,4,5,6,7,8,9,10,11,"lol","hello"]
c = enumerate(b)

with open("output.csv", "w", newline='') as myFile:
    print("Writing CSV")
    writer = csv.writer(myFile)
    writer.writerows(c)

print(list(c))

output:

>>>Writing CSV
>>>[]
>>>[Finished in 0.1s

If I preform: print(list(c)) before the writing method, c also becomes empty.

Thanks!

yurikleb
  • 180
  • 10

3 Answers3

4
c = enumerate(b)

Here c is not list but a generator, which is consumed when you iterate over it.

You will have to create new generator every time you use it.

If you want a permanent reference to the exhausted contents of the generator, you have to convert it to list.

c = list(enumerate(b))
jpp
  • 159,742
  • 34
  • 281
  • 339
Rahul
  • 10,830
  • 4
  • 53
  • 88
1

That is perfectly normal. c is a generator that will iterate over all elements of b, so it will iterate over b only once. That is when you call writer.writerows(c).

After that, the generator is depleted so making a list out of it will return an empty list.

Guillaume
  • 5,497
  • 3
  • 24
  • 42
1

[Python]: enumerate(iterable, start=0) returns a generator.

From [Python]: Generators:

The performance improvement from the use of generators is the result of the lazy (on demand) generation of values, which translates to lower memory usage. Furthermore, we do not need to wait until all the elements have been generated before we start to use them. This is similar to the benefits provided by iterators, but the generator makes building iterators easy.
...
Note: a generator will provide performance benefits only if we do not intend to use that set of generated values more than once.

The values of a generator are consumed once it's iterated on. That's why you have to "save" it by e.g. converting it to a list (which will also consume it since it will iterate over it).

For more details, you could also check [SO]: How do I list all files of a directory? (@CristiFati's answer - Part One) (Preliminary notes section - where I illustrate the behavior of [Python]: map(function, iterable, ...)).

CristiFati
  • 38,250
  • 9
  • 50
  • 87