Here is how you would do this with a nested list comprehension:

[[float(y) for y in x] for x in l]

This would give you a list of lists, similar to what you started with except with floats instead of strings.

If you want one flat list, then you would use

[float(y) for x in l for y in x]

Note the loop order - for x in l comes first in this one.

Answer from Andrew Clark on Stack Overflow
🌐
Built In
builtin.com β€Ί data-science β€Ί nested-list-comprehension-python
How to Write Nested List Comprehensions in Python | Built In
A nested list comprehension in Python is a list comprehension placed inside another list comprehension. It's useful for manipulating lists of lists, as it can combine multiple for loops, if statements and expressions into one line of code.
🌐
Spapas
spapas.github.io β€Ί 2016 β€Ί 04 β€Ί 27 β€Ί python-nested-list-comprehensions
Understanding nested list comprehension syntax in Python β€” /var/
April 27, 2016 - One final, more complex example: Let’s say that we have a list of lists of words and we want to get a list of all the letters of these words along with the index of the list they belong to but only for words with more than two characters. Using the same for-loop syntax for the nested list comprehensions we’ll get:
Discussions

Does anybody else just not like the syntax for nested one-line list comprehension?
I agree - I find the nested syntax difficult to sort out every time I want to use it. But I know others who find it very natural. More on reddit.com
🌐 r/Python
98
354
August 6, 2022
How can I use a nested list comprehension in Python to process a nested list? - TestMu AI Community
How can I use a nested list comprehension in Python to process a nested list? I have the following nested list: l = [[β€˜40’, β€˜20’, β€˜10’, β€˜30’], [β€˜20’, β€˜20’, β€˜20’, β€˜20’, β€˜20’, β€˜30’, β€˜20’], [β€˜30’, β€˜20’, β€˜30’, β€˜50’, β€˜10’, β€˜30’, ... More on community.testmuai.com
🌐 community.testmuai.com
0
December 3, 2024
Is there an official name for a list comprehension with multiple for clauses?
This is a nested for loop that builds a nested list: transposed = [] for i in range(4): transposed_row = [] for row in matrix: transposed_row.append(row[i]) transposed.append(transposed_row) And this is the equivalent nested list comprehension according to the documentation: transposed = [[row[i] ... More on discuss.python.org
🌐 discuss.python.org
0
February 13, 2025
Help understanding nested list comprehensions
Hi, i’m trying to wrap my head around this code that flattens the dict values lists into one list: d = { 'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]} result = [item for sublist in d.values() for item in sublist] print(result) result: [1, 2, 3, 4, 5, 6, 7, 8, 9] i’m a little confused ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
November 23, 2020
Top answer
1 of 12
516

Here is how you would do this with a nested list comprehension:

[[float(y) for y in x] for x in l]

This would give you a list of lists, similar to what you started with except with floats instead of strings.

If you want one flat list, then you would use

[float(y) for x in l for y in x]

Note the loop order - for x in l comes first in this one.

2 of 12
406

Here is how to convert nested for loop to nested list comprehension for your example:

newList = []
for x in l:
    for y in x:
        newList.append(float(y))


newList = [
    float(y)
    for x in l
        for y in x
]

Some other examples

students = [
    {"name": "John", "age": 18, "friends": ["Alice", "Bob"]},
    {"name": "Alice", "age": 19, "friends": ["John", "Doe"]},
]

# Normal for loop
friends = []
for student in students:
    for friend_name in student["friends"]:
        friends.append(friend_name)

# List comprehension
friends = [
    friend_name
    for student in students
    for friend_name in student["friends"]
]

For your case, if you want a flat list, it will be something like this.

new_list = [float(y) for x in l for y in x]

Another interesting example with if conditions in the mix:

school_data = [
    {
        "students": [
            {"name": "John", "age": 18, "friends": ["Alice", "Bob"]},
            {"name": "Alice", "age": 19, "friends": ["John", "Bob"]},
        ],
        "teacher": "Mr. Smith",
    },
    {
        "students": [
            {"name": "Sponge", "age": 20, "friends": ["Bob"]},
        ],
        "teacher": "Mr. tom",
    },
]

result = []

for class_dict in school_data:
    for student_dict in class_dict["students"]:
        if student_dict["name"] == "John" and student_dict["age"] == 18:
            for friend_name in student_dict["friends"]:
                if friend_name.startswith("A"):
                    result.append(friend_name)

And here is the listcomp version

result = [
    friend_name
    for class_dict in school_data
        if class_dict["teacher"] == "Mr. Smith"
            for student_dict in class_dict["students"]
                if student_dict["name"] == "John" and student_dict["age"] == 18
                    for friend_name in student_dict["friends"]
                        if friend_name.startswith("A")
]
# result => ['Alice']
🌐
LearnDataSci
learndatasci.com β€Ί solutions β€Ί python-list-comprehension
Python List Comprehension: single, multiple, nested, & more – LearnDataSci
It's generally agreed that multiple list comprehensions shouldn't go any deeper than two levels; otherwise, it could heavily sacrifice readability. To prove the point, here's how we could use for loops instead to solve the problem above: new_list = [] for nested_list in list_of_lists: # iterate through each nested list in the parent list converted_sub_list = [] for sub_list in nested_list: # for each nested list, iterate through the sub lists converted_sub_list.append([int(num) for num in sub_list]) new_list.append(converted_sub_list) # store converted sub list then move on new_list
🌐
Reuven Lerner
lerner.co.il β€Ί home β€Ί blog β€Ί python β€Ί understanding nested list comprehensions in python
Understanding nested list comprehensions in Python β€” Reuven Lerner
May 2, 2019 - That’s great if I want my list comprehension to return something based on each line of /etc/passwd. But each line of /etc/passwd is a string, and thus also iterable. Maybe I want to return something not based on the lines of the file, but on the characters of each line. Were I to use a β€œfor” loop to process the file, I would use a nested loop β€” i.e., one loop inside of the other, with the outer loop iterating over lines and the inner loop iterating over consonants.
🌐
Reddit
reddit.com β€Ί r/python β€Ί does anybody else just not like the syntax for nested one-line list comprehension?
r/Python on Reddit: Does anybody else just not like the syntax for nested one-line list comprehension?
August 6, 2022 -

Suppose I want to do some list comprehension:

new_list = [x*b for x in a]

Notice how you can easily tell what the list comprehension is doing by just reading left to right - i.e., "You calculate the product of x and b for every x in a."

Now, consider nested list comprehension. This code here which flattens a list of lists into just a single list:

[item for sublist in list_of_lists for item in sublist]  

Now, read this line of code:

[i*y for f in h for i in f]

Is this not clearly more annoying to read than the first line I posted? In order to tell what is going on you need to start at the left, then discern what i is all the way at the right, then discern what f is by going back to the middle.

If you wanted to describe this list comprehension, you would say: "Multiply i by y for every i in every f in h." Hence, this feels much more intuitive to me:

[item for item in sublist for sublist in list_of_lists]

[i*y for i in f for f in h] 

In how I have it, you can clearly read it left to right. I get that they were trying to mirror the structure of nested for loops outside of list comprehension:

for sublist in list_of_lists:
    for item in sublist:
        item

... however, the way that list comprehension is currently ordered still irks me.

Has anyone else been bothered by this?

Find elsewhere
🌐
Medium
medium.com β€Ί analytics-vidhya β€Ί writing-python-nested-list-comprehensions-in-3-steps-75bd9a1b79b6
Writing Python Nested List Comprehensions in 3 Steps | by Dan Eder | Analytics Vidhya | Medium
January 29, 2020 - In other words, we’re telling python to give us a new list of all the numbers from first_ten where the conditional is true. Another way of writing this is: evens = []for number in first_ten: if number % 2 == 0: evens.append(number) The difference with the list comprehension is you’ve accomplished the exact same thing in one line as opposed to four. Feels pretty great, right? You might want to be sitting down for this next part. Just as we nested an β€œif” conditional inside of the β€œfor” loop for our longer version of the above problem, you can also nest one list comprehension inside of another.
🌐
Skyfield
rhodesmill.org β€Ί brandon β€Ί 2009 β€Ί nested-comprehensions
I finally understand nested comprehensions
March 11, 2009 - My mistake in reading the multiple for clauses was that, old C-language programmer that I am, I was expecting the comprehension structure to be concentric. That is, I thought that the last for must β€œenclose” the ones above it, creating a mess of lists inside of lists inside of lists. But it turns out that they are much simpler to read than that. Just read them like normal loops, with the β€œbig loop” described first and the subsequent loops nested inside of it: #!python # The list comprehension said: [ expression for line in open('arecibo.txt') for char in line.strip() ] # It therefore meant: for line in open('arecibo.txt'): for char in line.strip(): list.append(expression)
🌐
Quora
quora.com β€Ί How-do-you-write-nested-list-comprehensions-in-Python
How to write nested list comprehensions in Python - Quora
Python (programming langu... ... Nested list comprehensions express loops (and optional filters) inside a single expression. They follow the same order as nested for-loops and can be read left-to-right as the outer loop to inner loop(s).
🌐
W3Schools
w3schools.com β€Ί python β€Ί python_lists_comprehension.asp
Python - List Comprehension
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
🌐
TestMu AI Community
community.testmuai.com β€Ί ask a question
How can I use a nested list comprehension in Python to process a nested list? - TestMu AI Community
December 3, 2024 - How can I use a nested list comprehension in Python to process a nested list? I have the following nested list: l = [[β€˜40’, β€˜20’, β€˜10’, β€˜30’], [β€˜20’, β€˜20’, β€˜20’, β€˜20’, β€˜20’, β€˜30’, β€˜20’], [β€˜30’, β€˜20’, β€˜30’, β€˜50’, β€˜10’, β€˜30’, ...
🌐
Python.org
discuss.python.org β€Ί python help
Is there an official name for a list comprehension with multiple for clauses? - Python Help - Discussions on Python.org
February 13, 2025 - This is a nested for loop that ... this is the equivalent nested list comprehension according to the documentation: transposed = [[row[i] ......
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί nested-list-comprehensions-in-python
Nested List Comprehensions in Python - GeeksforGeeks
July 5, 2025 - Explanation: This code builds a 5x5 matrix (list of lists), where each row contains numbers from 0 to 4 using nested loops. ... Explanation: A more concise version of the same logic, the inner loop [c for c in range(5)] creates each row and ...
🌐
Python Morsels
pythonmorsels.com β€Ί nested-list-comprehensions
Nested list comprehensions - Python Morsels
September 17, 2025 - And when I see nested comprehensions, I think "we're building a list-of-lists": >>> percentages = [ ... [score/100 for score in group_scores] ... for group_scores in scores_by_group ... ] Python's for loops showcase the looping, while comprehensions showcase the list-making.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-list-comprehension
List Comprehension in Python - GeeksforGeeks
This example produces every possible (x, y) pair where both x and y range from 0 to 2. ... A nested list (matrix) can be transformed into a single flat list by iterating through each sublist and its elements.
Published Β  September 20, 2025
🌐
Medium
mowbray-chad.medium.com β€Ί understanding-nested-list-comprehensions-in-python-6fd6be8ce8ec
Understanding Nested List Comprehensions in Python | by Chad Mowbray | Medium
December 14, 2020 - Understanding Nested List Comprehensions in Python Flatten multidimensional arrays using Python’s list comprehensions If you feel like you understand this, [x for x in range(10)] but are at a loss …
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί can anyone explain nested list comprehensions?
r/learnpython on Reddit: Can anyone explain nested list comprehensions?
October 26, 2019 -

I know the top comment is going to be ~ Never reduce lines of code at the expense of readability; I wholeheartedly agree. Unfortunately, at my work, I'm sorting through quite a few nested list comprehensions and I'm having a hard time making sense of their structure.

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = [item for sublist in nested_list for item in sublist]
print(flattened_list)
>>>> [1, 2, 3, 4, 5, 6, 7, 8, 9] 

What really trips me up is the for item in sublist after nested_list is referenced. If nested lists were 3 layers deep instead of 2, I have no idea what that should look like. Which makes me feel that I can memorize the pattern, but I don't really understand its rules.

🌐
Python documentation
docs.python.org β€Ί 3 β€Ί tutorial β€Ί datastructures.html
5. Data Structures β€” Python 3.14.3 documentation
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses ...