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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:07:54+00:00 2026-06-17T14:07:54+00:00

When iterating over the following generator expression, fun(i) for i in mylist if i

  • 0

When iterating over the following generator expression,

fun(i) for i in mylist if i not in setA.union(setB)

is the setA.union method called in each iteration, or only once?

  • 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-17T14:07:55+00:00Added an answer on June 17, 2026 at 2:07 pm

    Simple proof using dis module:

    In [24]: def func():
       ....:     a=set([1,2,3])
       ....:     b=set([3,4,5])
       ....:     c=[i for i in xrange(10) if i in a.union(b)]
       ....:     
    
    In [25]: dis.dis(func)
    
      4          42 BUILD_LIST               0
                 45 LOAD_GLOBAL              1 (xrange)
                 48 LOAD_CONST               6 (10)
                 51 CALL_FUNCTION            1
                 54 GET_ITER                          #iterator returned from xrange
    
            >>   55 FOR_ITER                33 (to 91) #until the iterator is not exhausted
                 58 STORE_FAST               2 (i)
                 61 LOAD_FAST                2 (i)
                 64 LOAD_FAST                0 (a)
                 67 LOAD_ATTR                2 (union)
                 70 LOAD_FAST                1 (b)
                 73 CALL_FUNCTION            1      #union() is being called in each iteration
                 76 COMPARE_OP               6 (in)
                 79 POP_JUMP_IF_FALSE       55
                 82 LOAD_FAST                2 (i)
                 85 LIST_APPEND              2
                 88 JUMP_ABSOLUTE           55
                                                             #end of loop    
            >>   91 STORE_FAST               3 (c)
                 94 LOAD_CONST               0 (None)
                 97 RETURN_VALUE 
    

    So, for your example it is going to be called in each iteration i.e. len(mylist) times.

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

Sidebar

Related Questions

Following is a method that is iterating over a list: @Override public void DataRowMapper(List<?>
I wrote the following code for iterating over a treemap using the entryset method.
I have the following issue related to iterating over an associative array of strings
I want avoid iterating over a nil array. My bad solution: if nil!=myArr myArr.each
I have the following that is iterating over an array of templateOverrides. DPGlobal.template is
How would I go about iterating over the following foreach with a delay between
I am iterating over input elements using .each() and want to get class of
I have the following code in a loop iterating over the different document objects:
The following code is commonly seen on SO when it comes to iterating over
I am getting a UserList by following List<User> UserList=usersService.findUsers(); than am iterating over this

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.