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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:57:55+00:00 2026-06-16T14:57:55+00:00

Apologies for the somewhat vague title, I’ll try explain more here. Currently, I have

  • 0

Apologies for the somewhat vague title, I’ll try explain more here.

Currently, I have the following code, which counts the number of times the values “y” and “n” show up in the list called “results”.

NumberOfA = results.count("y")
NumberOfB = results.count("n")

Is there a way of making it so that, for example, values such as “yes” are also counted towards NumberOfA? I was thinking something along the lines of the following:

NumberOfA = results.count("y" and "yes" and "Yes")
NumberOfB = results.count("n" and "no" and "No")

But that doesn’t work. This is probably a pretty easy one to solve, but hey. Thanks in advance!

  • 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-16T14:57:56+00:00Added an answer on June 16, 2026 at 2:57 pm

    As for why your answer above does not work, it is because Python will just take the final value of the expression you pass in:

    >>> 'Yes' and 'y' and 'yes'
    'yes'
    

    Therefore your count will be off because it is just looking for that final value:

    >>> results.count('yes' and 'y')
    1
    >>> results.count('yes' and '???')
    0
    

    Would something like this work? Note that this depends on their being only yes/no-esque answers in the list (will be wrong if things like ‘Yeah….um no’ are in there):

    In [1]: results = ['yes', 'y', 'Yes', 'no', 'NO', 'n']
    
    In [2]: yes = sum(1 for x in results if x.lower().startswith('y'))
    
    In [3]: no = sum(1 for x in results if x.lower().startswith('n'))
    
    In [4]: print yes, no
    3 3
    

    The general idea is to take your results list and then iterate through each item, lowercasing it and then taking the first letter (startswith) – if the letter is a y, we know it is a yes; otherwise, it will be no.

    You can also combine the steps above if you want by doing something like this (note this requires Python 2.7):

    >>> from collections import Counter
    >>> results = ['yes', 'y', 'Yes', 'no', 'NO', 'n']
    >>> Counter((x.lower()[0] for x in results))
    Counter({'y': 3, 'n': 3})
    

    Counter objects can be treated just like dictionaries, so you would now essentially have a dictionary that contained the counts of yes‘s and no‘s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies for the somewhat vague title, I can't work out what the keywords are
Apologies for the poor question title. I have two tables, jobs and Persons-Jobs. Jobs
Apologies, somewhat confused Python newbie question. Let's say I have a module called animals.py
(apologies for the somewhat lengthy intro) During development of an application which prefaults an
Apologies for the somewhat unclear question - couldn't think of a better way of
Apologies, It looks like my original question was not able to correctly explain what
First up, apologies for the length of this question! I have a page with
I apologize for the somewhat confusing question, I'm not sure how to explain it
I apologize in advance for this somewhat ignorant question, but I have researched this
Apologies for the big block 'o' text, but I'm somewhat of a beginner having

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.