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

  • Home
  • SEARCH
  • 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 7509259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:51:07+00:00 2026-05-29T22:51:07+00:00

I have tried: >>> l = [1,2,3] >>> x = 1 >>> x in

  • 0

I have tried:

>>> l = [1,2,3]
>>> x = 1
>>> x in l and lambda: print("Foo")
    x in l && print "Horray"
            ^
SyntaxError: invalid syntax

A bit of googling revealed that print is a statement in python2 whereas it’s a function in python3. But, I have tried the above snipped in python3 and it throws SyntaxError exception.

Any idea on how can I do it in one line? (Readability or google programming practice is not an issue here)

  • 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-29T22:51:08+00:00Added an answer on May 29, 2026 at 10:51 pm

    lambda creates, well a lambda. It needs to be called to execute it. You cannot do this that way, because Python doesn’t allow statements in this context, only expressions (including function calls).

    To make print a function in Python 2.x, try:

    from __future__ import print_function
    x in l and print('foo')
    

    Be wary though. If you try:

    x in l and print('foo') or print('bar')
    

    it won’t work, because print returns None, so the first and expression is False, so both prints will be executed. In Python 3.x you don’t need the import.

    If you won’t have complex short-circuiting (i.e. just one and or or), or you know your functions or expressions won’t surprise the short-circuiting logic, there’s nothing wrong with the code. Otherwise, try the non-short-circuiting 1-liner:

    print('foo') if x in l else print('bar')
    

    This form is recommended only if the probability/expectation of the conditional to be True is vastly higher than being False. Otherwise, plain good-old if-else is the way to go.

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

Sidebar

Related Questions

Boost lambda allows to overwrite deduced return type using ret<T> template. I have tried
I just have tried to compare performance of lambda expressions in C++11, so I
I have a lambda expression that gets results from a Dictionary. var sortedDict =
I have tried, and burned my brain to understand the definition of Regular Languages
Can any body translate the following c# code to vb. I have tried telarik
I have some serial code like this that computes word concordances i.e. counting collocated
I have a database of sports teams that I am displaying in tables/standings. The
I have a class that builds a url with query string parameters and so
I have a program where I must print many STL vectors on the screen
I have a Haskell Map, containing strings as keys and some lambda functions as

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.