Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9013555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:17:52+00:00 2026-06-16T03:17:52+00:00

While researching options for an oft-asked question (How to find the number of rows

  • 0

While researching options for an oft-asked question (How to find the number of rows in a CSV reader before iterating), I came across the approach of using len(list(myCSVReader)). Yes, I know how klunky and potentially expensive this is, and I won’t be using it. But while playing with it, I came across a really puzzling inconsistency:

#myFile.txt is a 3-line CSV file
myCSV = csv.reader(open('myFile.txt','rb'),dialect='excel')
print(len(list(myCSV)))
print(list(myCSV))

When I run this, I get:

> 3
> []

I can understand why you couldn’t just use list() to convert the reader to a list of lists, but if that’s the case, then how/why does a len() on this produce the correct result?

— JDM

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:17:53+00:00Added an answer on June 16, 2026 at 3:17 am

    This is what happens when you build a list from a generator.

    • Generator is an iterable object that yields items one by one. At some point it is exhausted.

    • csv.reader returns a generator.

    • When you do list(generator), the list constructor consumes all items from the generator.

    • Next time you try getting something from the generator (e.g. do list(generator) another time), it’s already exhausted.

    What you can do is
    create a list once and use it where needed:

    myCSV = list(csv.reader(open('myFile.txt','rb'),dialect='excel'))
    print(len(myCSV))
    print(myCSV)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While researching the issue of JSON vs XML , I came across this question
While researching Oracle Analytics, I came across this query: select an_id, a_date, min(a_date) over
While researching the use of Table Hints , I came across these two questions:
While researching how to detect null values in an array , I came across
I came across this really nice looking Scala code while researching XMPP for a
I've been researching for a while now trying to find a reason why the
While researching how to create custom compound views in Android, I have come across
I noticed an interesting result from JSLint while researching a codereview question. JSLint complained
After researching my options with regards to video support in Java, I stumbled across
I stumbled upon this post while researching on the Delete Directory options available under:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.