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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:35:23+00:00 2026-06-10T00:35:23+00:00

I was answering this question , I preferred generator expression here and used this,

  • 0

I was answering this question, I preferred generator expression here and used this, which I thought would be faster as generator doesn’t need to create the whole list first:

>>> lis=[['a','b','c'],['d','e','f']]
>>> 'd' in (y for x in lis for y in x)
True

And Levon used list comprehension in his solution,

>>> lis = [['a','b','c'],['d','e','f']]
>>> 'd' in [j for i in mylist for j in i]
True

But when I did the timeit results for these LC was faster than generator:

~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f']]" "'d' in (y for x in lis for y in x)"
    100000 loops, best of 3: 2.36 usec per loop
~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f']]" "'d' in [y for x in lis for y in x]"
    100000 loops, best of 3: 1.51 usec per loop

then I increased the size of list, and timed it again:

lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]

This time for searching 'd' generator was faster than LC, but when I searched a middle element(11) and the last element then LC again beats the generator expression, and I can’t understand why?

~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "'d' in (y for x in lis for y in x)"
    100000 loops, best of 3: 2.96 usec per loop

~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "'d' in [y for x in lis for y in x]"
    100000 loops, best of 3: 7.4 usec per loop

~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "11 in [y for x in lis for y in x]"
100000 loops, best of 3: 5.61 usec per loop

~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "11 in (y for x in lis for y in x)"
100000 loops, best of 3: 9.76 usec per loop

~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "18 in (y for x in lis for y in x)"
100000 loops, best of 3: 8.94 usec per loop

~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "18 in [y for x in lis for y in x]"
100000 loops, best of 3: 7.13 usec per loop
  • 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-10T00:35:25+00:00Added an answer on June 10, 2026 at 12:35 am

    Expanding on Paulo‘s answer, generator expressions are often slower than list comprehensions because of the overhead of function calls. In this case, the short-circuiting behavior of in offsets that slowness if the item is found fairly early, but otherwise, the pattern holds.

    I ran a simple script through the profiler for a more detailed analysis. Here’s the script:

    lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],
         [7,8,9],[10,11,12],[13,14,15],[16,17,18]]
    
    def ge_d():
        return 'd' in (y for x in lis for y in x)
    def lc_d():
        return 'd' in [y for x in lis for y in x]
    
    def ge_11():
        return 11 in (y for x in lis for y in x)
    def lc_11():
        return 11 in [y for x in lis for y in x]
    
    def ge_18():
        return 18 in (y for x in lis for y in x)
    def lc_18():
        return 18 in [y for x in lis for y in x]
    
    for i in xrange(100000):
        ge_d()
        lc_d()
        ge_11()
        lc_11()
        ge_18()
        lc_18()
    

    Here are the relevant results, reordered to make the patterns clearer.

             5400002 function calls in 2.830 seconds
    
       Ordered by: standard name
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       100000    0.158    0.000    0.251    0.000 fop.py:3(ge_d)
       500000    0.092    0.000    0.092    0.000 fop.py:4(<genexpr>)
       100000    0.285    0.000    0.285    0.000 fop.py:5(lc_d)
    
       100000    0.356    0.000    0.634    0.000 fop.py:8(ge_11)
      1800000    0.278    0.000    0.278    0.000 fop.py:9(<genexpr>)
       100000    0.333    0.000    0.333    0.000 fop.py:10(lc_11)
    
       100000    0.435    0.000    0.806    0.000 fop.py:13(ge_18)
      2500000    0.371    0.000    0.371    0.000 fop.py:14(<genexpr>)
       100000    0.344    0.000    0.344    0.000 fop.py:15(lc_18)
    

    Creating a generator expression is equivalent to creating a generator function and calling it. That accounts for one call to <genexpr>. Then, in the first case, next is called 4 times, until d is reached, for a total of 5 calls (times 100000 iterations = ncalls = 500000). In the second case, it is called 17 times, for a total of 18 calls; and in the third, 24 times, for a total of 25 calls.

    The genex outperforms the list comprehension in the first case, but the extra calls to next account for most of the difference between the speed of the list comprehension and the speed of the generator expression in the second and third cases.

    >>> .634 - .278 - .333
    0.023
    >>> .806 - .371 - .344
    0.091
    

    I’m not sure what accounts for the remaining time; it seems that generator expressions would be a hair slower even without the additional function calls. I suppose this confirms inspectorG4dget‘s assertion that “creating a generator comprehension has more native overhead than does a list comprehension.” But in any case, this shows pretty clearly that generator expressions are slower mostly because of calls to next.

    I’ll add that when short-circuiting doesn’t help, list comprehensions are still faster, even for very large lists. For example:

    >>> counter = itertools.count()
    >>> lol = [[counter.next(), counter.next(), counter.next()] 
               for _ in range(1000000)]
    >>> 2999999 in (i for sublist in lol for i in sublist)
    True
    >>> 3000000 in (i for sublist in lol for i in sublist)
    False
    >>> %timeit 2999999 in [i for sublist in lol for i in sublist]
    1 loops, best of 3: 312 ms per loop
    >>> %timeit 2999999 in (i for sublist in lol for i in sublist)
    1 loops, best of 3: 351 ms per loop
    >>> %timeit any([2999999 in sublist for sublist in lol])
    10 loops, best of 3: 161 ms per loop
    >>> %timeit any(2999999 in sublist for sublist in lol)
    10 loops, best of 3: 163 ms per loop
    >>> %timeit for i in [2999999 in sublist for sublist in lol]: pass
    1 loops, best of 3: 171 ms per loop
    >>> %timeit for i in (2999999 in sublist for sublist in lol): pass
    1 loops, best of 3: 183 ms per loop
    

    As you can see, when short circuiting is irrelevant, list comprehensions are consistently faster even for a million-item-long list of lists. Obviously for actual uses of in at these scales, generators will be faster because of short-circuiting. But for other kinds of iterative tasks that are truly linear in the number of items, list comprehensions are pretty much always faster. This is especially true if you need to perform multiple tests on a list; you can iterate over an already-built list comprehension very quickly:

    >>> incache = [2999999 in sublist for sublist in lol]
    >>> get_list = lambda: incache
    >>> get_gen = lambda: (2999999 in sublist for sublist in lol)
    >>> %timeit for i in get_list(): pass
    100 loops, best of 3: 18.6 ms per loop
    >>> %timeit for i in get_gen(): pass
    1 loops, best of 3: 187 ms per loop
    

    In this case, the list comprehension is an order of magnitude faster!

    Of course, this only remains true until you run out of memory. Which brings me to my final point. There are two main reasons to use a generator: to take advantage of short circuiting, and to save memory. For very large seqences/iterables, generators are the obvious way to go, because they save memory. But if short-circuiting is not an option, you pretty much never choose generators over lists for speed. You chose them to save memory, and it’s always a trade-off.

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

Sidebar

Related Questions

This is all I need to finish answering my last question . If you
Before answering this question, understand that I am not asking how to create my
When answering this question I made some research which really confuses me. I noticed
I have never used JodaTime before, but answering this question, How to get ordinal
While answering this question I found some strange behavior for which I have no
In answering this question , it got me thinking... I often use this pattern:
While answering this question, I got these confusing results: double d = 0.49999999999999990d; //output
I'm asking and answering this question so that I can find it again in
Please bear in mind that I'm totally new to Rails when answering this.My question
In answering this code golf question , I ran across a problem in my

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.