Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Flore W4,743 Points
Why do we need rows = list(artreader)?
The code in the video is:
with open('museum.csv',' newline ='') as csvfile:
artreader = csv.DictReader(csvfile, delimiter='|')
rows = list(artreader)
for row in rows:
and I get all the artifact numbers.
If I delete the line 'rows = list(artreader)' and replace 'for row in rows' by 'for row in artreader', I get exactly the same result. So why do I need it?
Chris FreemanTreehouse Moderator 68,423 Points
You are correct, in that, you code is equivalent. The object
artreader is an iterator so it can be used directly as the target of a
for loop. One drawback on iterators is they can only be used once. Expanding the iterator into a the list
rows might be useful if the code intends to use the data multiple times.