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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:44:33+00:00 2026-06-11T20:44:33+00:00

As a contrived example: myset = set([‘a’, ‘b’, ‘c’, ‘d’]) mydict = {item: (yield

  • 0

As a contrived example:

myset = set(['a', 'b', 'c', 'd'])
mydict = {item: (yield ''.join([item, 's'])) for item in myset}

and list(mydict) gives:

['as', 'cs', 'bs', 'ds', {'a': None, 'b': None, 'c': None, 'd': None}]

What happens here? What does yield do? And is this behavior consistent no matter what expression follows yield?

Note: I know that doing mydict = {item: ''.join([item, 's']) for item in myset} would give the dictionary {'a': 'as', 'b': 'bs', 'c': 'cs', 'd': 'ds'}, which seems to be what I am trying to do 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-06-11T20:44:34+00:00Added an answer on June 11, 2026 at 8:44 pm

    First of all, what does yield return? The answer in this case is None, because yield returns the parameter passed to next(), which is nothing in this case (list doesn’t pass anything to next).

    Now here’s your answer:

    >>> myset = set(['a', 'b', 'c', 'd'])
    >>> mydict = {item: (yield ''.join([item, 's'])) for item in myset}
    >>> mydict
    <generator object <dictcomp> at 0x0222BB20>
    

    The dict comprehension is turned into a generator, because you used yield in a function body context! This means that the whole thing isn’t evaluated until it’s passed into list.

    So here’s what happens:

    1. list calls next(mydict).
    2. Yield returns ''.join([item, 's']) to list and freezes the comprehension.
    3. list calls next(mydict).
    4. The comprehension resumes and assigns the result of yield (None) to item in the dictionary and starts a new comprehension iteration.
    5. Go back to 1.

    And at last the actual generator object returns the temporary in the body, which was the dict. Why this happens is unknown to me, and it’s probably not documented behaviour either.

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

Sidebar

Related Questions

Here's a contrived example: I want to divide two numbers and I need to
To use a contrived example in Java, here's the code: enum Commands{ Save(S); File(F);
Here's a contrived example of my code: <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
Here is a contrived example of an xml document. In my real world case,
Here is my contrived example that illustrates what I am attempting to accomplish. I
So here's a contrived example of what I want to have improved or confirmed.
This is a somewhat contrived example meant to show a point. Here are two
so, here's a bit of a contrived example: trait MyTrait { type T <:
Here's an contrived example of what's going on: http://jsfiddle.net/adamjford/YNGcm/20/ HTML: <a href=#>Click me!</a> <div></div>
Here is a contrived example of how a lot of our classes return binary

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.