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 7767827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:46:14+00:00 2026-06-01T15:46:14+00:00

I’m a newbie in Python and I’m studying list comprehension. What I’m trying to

  • 0

I’m a newbie in Python and I’m studying list comprehension.

What I’m trying to do is to convert the following code into list comprehension:

def words_without_e():
    count = 0

    words = open('words.txt')
    for word in words:
        if word.find('e') == -1:
            count += 1
    words.close()

    return count

Here’s my feeble attempt:

words = open('words.txt')
print sum([1 for word in words if word.find('e') == -1])

But unfortunately it’s not working. The answer I expect to get is 37641, but I’m getting 0. 🙁

I tried creating another code doing the same thing but instead of file as source, I used a list:

def test():
    words = ['hello', 'world', 'ciao']
    return sum([1 for word in words if word.find('e') == -1])

And it works.

I saw this “quite” similar SO post and tried the code posted there return len([word for word in words if len(word) >= 2 and word[0] == word[-1]]). It works if the source is a hard-coded list but fails if the source is an external file.

Now, my question is, does sum only works with lists and tuples? If I understood the docs correctly, any iterable could be summed up.

Any enlightenment would be very much appreciated. 🙂

  • 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-01T15:46:15+00:00Added an answer on June 1, 2026 at 3:46 pm

    The simplest solution is this:

    with open("words.txt") as words:
      sum(1 for word in words if "e" not in word)
    

    As you can see, sum does work with any iterator – here I am using a generator expression.

    Rather than doing word.find('e') == -1 we can just do "e" not in word which is nicer to read and works as strings are iterable themselves and support __contains__.

    I am also using the with statement to open files – this is preferable to manually opening and closing them as it handles those things for you, and handles exceptions correctly too.

    I would like to note however, your example works for me. My guess is that your file is space or comma delimited, but looping through a file returns lines.

    My test file:

    bob
    bill
    james
    test
    something
    no
    

    This, for example, will not work:

    bob bill james test something no
    

    As we will get one string containing the whole thing. In this case, we can use str.split() to split the lines into words.

    E.g:

    with open("words.txt") as lines:
        sum(1 for line in lines for word in line.split() if "e" not in word)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.