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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:04:17+00:00 2026-05-27T23:04:17+00:00

Picking up a tip from someone else on this site, I’ve been using logical

  • 0

Picking up a tip from someone else on this site, I’ve been using logical operators to express conditional logic in list comprehension as a shortcut to using full for loops with if statements.

For example, if I had a list [1, 0, 0, 1, 1] that I wanted to turn into [“Yes”, “No”, “No”, “Yes”, “Yes”] I could do it like this:

original = [1, 0, 0, 1, 1]
new = [((x==1) and "Yes") or "No" for x in original]

The problem I have is that I get some very strange results when I use the logical operators with 0 in this way. The results occur regardless of whether I’m using list comprehension or not.

These:

print((True and 1.0) or "Wrong")
print((True and -1.0) or "Wrong")
print((True and "1.0") or "Wrong")
print((True and 1) or "Wrong")

Correctly (I think) output:

1.0
-1.0
1.0
1

However these:

print((True and 0) or "Wrong")
print((True and 0.0) or "Wrong")

Output:

Wrong
Wrong

I think that Python is implicitly converting both 0 and 0.0 to a boolean False, and any other value of any other type to True

Can someone confirm whether:
a. This is the case and/or
b. Is there a way to achieve what I’m after in list comprehension another way (with a lamda or something)

Many thanks
Chris

  • 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-05-27T23:04:18+00:00Added an answer on May 27, 2026 at 11:04 pm

    Yes 0 is returning a false in your short-circuit evaluation (you can test via bool(0) which returns a false ). I think this is a lot simpler:

    new = ["yes" if x else "no" for x in list]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone give me some pointers on picking up the user's MAC address from
I am using gallery picker for picking an image from gallery. The photos taken
I'm picking up information from a remote website and I'm stuck with this bit
I found this thread about picking the closest/nearest value from an array based upon
I'm using this code for picking contacts: contactsButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v)
Dear all from the below mentioned table im picking imgurl with this query but
I have noticed some developers picking up new skills and moving from one platform
I'm trying to implement picking using Pyglet's OpenGL wrapper, but I'm having trouble converting
Hello i have a question on picking random entries from a database. I have
An application that has been working well for months has stopped picking up the

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.