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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:54:03+00:00 2026-05-28T22:54:03+00:00

My question is about Python List Comprehension readability. When I come across code with

  • 0

My question is about Python List Comprehension readability. When I come across code with complex/nested list comprehensions, I find that I have to re-read them several times in order to understand the intent.

Is there an intuitive way to read aloud list comprehensions? Seems like I should start “reading” from the middle, then read the if conditions (if any), and read the expression last.

Here’s how I would read the follow line of code aloud, in order to understand it:

[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]

“For each element in List x, and each element in List y, if the two elements are not the same, create a list of tuples.”

Two examples that I am struggling with:
How would you read the following List Comprehensions aloud?

  1. From another question in Stack Overflow: [x for b in a for x in b]

  2. Python docs has this example:
    [[row[i] for row in matrix] for i in range(4)]

Any suggestions or pointers for ways to read aloud list comprehensions such that the intention becomes clearer is much appreciated.

  • 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-28T22:54:04+00:00Added an answer on May 28, 2026 at 10:54 pm

    I usually unfold it in my mind into a generating loop, so for example

    [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
    

    is the list comprehension for the generator

    for x in [1,2,3]:
        for y in [3,1,4]:
            if x != y:
                yield (x, y)
    

    Example #1

    [x for b in a for x in b] is the comprehension for

    for b in a:
        for x in b:
            yield x
    

    Example result for a = [[1,2,3],[4,5,6]]: [1, 2, 3, 4, 5, 6]


    Example #2

    [[row[i] for row in matrix] for i in range(4)] (note the inner expression is another comprehension!):

    for i in range(4):
        yield [row[i] for row in matrix]
    

    which is unfolded

    for i in range(4):
        l = []
    
        for row in matrix:
            l.append(row[i])
    
        yield l
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about python mechanize's proxy support. I'm making some web client
I have a question about using os.execvp in Python. I have the following bit
I'm currently developing some things in Python and I have a question about variables
So I have this code in python that writes some values to a Dictionary
I have a question about the virtual memory in Python. When the process is
I have a question about sort defaultdict in python Assume I have the following
I have a python script that creates a list of lists of server uptime
So I just learned about List Comprehensions in python. some of these are getting
Quick Python question: How do I access data from a nested list like this:
I have a question about the map function in Python. From what I understand,

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.