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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:12:22+00:00 2026-05-27T20:12:22+00:00

Possible Duplicate: Is it Pythonic to use list comprehensions for just side effects? proper

  • 0

Possible Duplicate:
Is it Pythonic to use list comprehensions for just side effects?
proper use of list comprehensions – python

Python has the useful and elegant list comprehension syntax. However AFAIK it always produces a list. Sometimes I feel the urge to use list comprehension just for its compactness and elegance without needing the resulting list:

[some_func(x) for x in some_list if x>5]

some_func() may return something which I don’t need, it may not return anything at all. I tried the generator syntax:

(some_func(x) for x in some_list if x>5)

but as you may guess, it doesn’t iterate over some_list. It does so only within certain context:

other_func(some_func(x) for x in some_list if x>5)

So… is there a syntax I’m missing to get this to work, or should I always fall back to 3 lines?

for x in some_list:
    if x>5:
        some_func(x)
  • 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-27T20:12:23+00:00Added an answer on May 27, 2026 at 8:12 pm

    I don’t now if you will find it elegant, but there is a consume recipe in the itertools docs that is very fast and will run an iterator to completion without building-up a list:

    >>> consume(some_func(x) for x in some_list if x>5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Python program to find fibonacci series. More Pythonic way. Hey, i was
Possible Duplicate: Pythonic way to find maximum value and its index in a list?
Possible Duplicate: How do you split a list into evenly sized chunks in Python?
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: Pythonic way to check if a file exists? How can Check if
Possible Duplicate: Where can I find a web-project “security checklist?” i was just wondering
Possible Duplicate: How do I make a list with checkboxes in Java Swing? I
Possible Duplicate: Unload a module in Python After importing Numpy, lets say I want
Possible Duplicate: how to use foursquare API in android application? This is a question

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.