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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:24:10+00:00 2026-05-23T07:24:10+00:00

Is filter/map equivalent to list comprehension? Suppose I have the following function def fib_gen():

  • 0

Is filter/map equivalent to list comprehension?
Suppose I have the following function

def fib_gen():
    a,b = 0,1
    yield 0
    yield 1
    while True:
        a,b = b,a+b
        yield b

Now I can use list comprehension to list fib numbers:

a = fib_gen()
print [a.next() for i in range(int(sys.argv[1]))]

Suppose I want to list only even fib numbers. I would do the following with filter/map:

a = fib_gen()
print filter(even, map(lambda x: a.next(), range(int(sys.argv[1]))))

How can I get the same result with list comprehension?

  • 1 1 Answer
  • 2 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-23T07:24:11+00:00Added an answer on May 23, 2026 at 7:24 am

    You could use a generator to store the intermediate result, and “filter” on it.

    fibs = (a.next() for i in whatever)
    even_fibs = [num for num in fibs if num % 2 == 0]
    

    or in one line:

    even_fibs = [num for num in (a.next() for i in whatever) if num % 2 == 0]
    

    Note that, if you want to take a definite number of elements from an iterator, you could use itertools.islice instead:

    from itertools import islice
    fibs_max_count = int(sys.argv[1])
    even_fibs = [num for num in islice(fib_gen(), fibs_max_count) if num%2 == 0]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When should you use map/filter instead of a list comprehension or generator expression?
Is it possible to combine Underscore's filter and map? I currently have two separate
I have a persistent map which I want to filter. Something like this: (filter
If I use map or filter inside another function, does that function become a
I would like to filter an array of items by using the map() function.
(define filter-in (lambda (predicate list) (let((f (lambda (l) (filter-in-sexpr predicate l)))) (map f list))))
Need to call a filter function on some options based on a radio selected
How we can filter the object in List<> in C#?
I'm trying to filter some XML in JavaScript using E4X and have some specific
I want to filter and search through Json using multiple keywords. I have it

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.