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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:36:44+00:00 2026-05-12T18:36:44+00:00

I like to use the following idiom for combining lists together, sometimes: >>> list(itertools.chain(*[[(e,

  • 0

I like to use the following idiom for combining lists together, sometimes:

>>> list(itertools.chain(*[[(e, n) for e in l] for n, l in (('a', [1,2]),('b',[3,4]))]))
[(1, 'a'), (2, 'a'), (3, 'b'), (4, 'b')]

(I know there are easier ways to get this particular result, but it comes comes in handy when you want to iterate over the elements in lists of lists of lists, or something like that. The trouble is, when you use generator expressions, this becomes error prone. E.g.

>>> list(itertools.chain(*(((e, n) for e in l) for n, l in (('a', [1,2]),('b',[3,4])))))
[(1, 'b'), (2, 'b'), (3, 'b'), (4, 'b')]

What’s happening here is that the inner generator expressions get passed as arguments to itertools.chain, so at the the time they’re evaluated, the outer generator expression has finished, and n is fixed at its final value, 'b'. I’m wondering whether anyone has thought of ways to avoid this kind of error, beyond “don’t do that.”

  • 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-12T18:36:44+00:00Added an answer on May 12, 2026 at 6:36 pm

    Your approach almost works, you just need to flatten the generators. See how the for e in l is moved to the very right

    >>> list(itertools.chain((e, n) for n, l in (('a', [1,2]),('b',[3,4])) for e in l ))
    [(1, 'a'), (2, 'a'), (3, 'b'), (4, 'b')]
    

    Here is how to do the same thing using itertools.product

    >>> X=itertools.chain(*(itertools.product(*i[::-1]) for i in (('a', [1,2]),('b',[3,4]))))
    >>> print list(X)
    [(1, 'a'), (2, 'a'), (3, 'b'), (4, 'b')]
    

    or if you are allowed to switch the tuples around

    >>> X=itertools.chain(*(itertools.product(*i) for i in (([1,2],'a'),([3,4],'b'))))
    >>> print list(X)
    [(1, 'a'), (2, 'a'), (3, 'b'), (4, 'b')]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to use the following idiom, that I think is non-standard. I have
I would like to use following sql to avoid constructing sql dynamically: SELECT CommentID,
I would like to use the following class across multiple modules without needing log
I'm writing my Servlet application and would like to use the following static method
Let's say I have a hashref constant like the following: use constant DOGS =>
I like to use GSON to deserialize the following JSON String. { type: FeatureCollection,
I would like to use KVO in the following context: 1) In touchesBegan:withEvent: I
I would like to use something like the following: SELECT city FROM cities WHERE
I'd like to use the .Join() method in LINQ to perform the following SQL:
I'd like to use the ASP.NET membership provider in the following (low-security) scenario... 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.