execution order of expressions in a list comprehensions [duplicate]












1
















This question already has an answer here:




  • Understanding nested list comprehension

    2 answers




Given the following expressions:



matrix = [[1,2,3],[4,5,6],[7,8,9]]


A matrix is created, then a list comprehension is executed to create a flat list. The comprehension runs from left to right.



flat = [x for row in matrix for x in row]


Subsequently for each row in the matrix its values are squared. How is this comprehension evaluated?



squared = [[x**2 for x in row] for row in matrix]









share|improve this question















marked as duplicate by roganjosh, juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 20:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • let me review that post... thanks for the reference

    – dcrearer
    Nov 21 '18 at 19:54
















1
















This question already has an answer here:




  • Understanding nested list comprehension

    2 answers




Given the following expressions:



matrix = [[1,2,3],[4,5,6],[7,8,9]]


A matrix is created, then a list comprehension is executed to create a flat list. The comprehension runs from left to right.



flat = [x for row in matrix for x in row]


Subsequently for each row in the matrix its values are squared. How is this comprehension evaluated?



squared = [[x**2 for x in row] for row in matrix]









share|improve this question















marked as duplicate by roganjosh, juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 20:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • let me review that post... thanks for the reference

    – dcrearer
    Nov 21 '18 at 19:54














1












1








1









This question already has an answer here:




  • Understanding nested list comprehension

    2 answers




Given the following expressions:



matrix = [[1,2,3],[4,5,6],[7,8,9]]


A matrix is created, then a list comprehension is executed to create a flat list. The comprehension runs from left to right.



flat = [x for row in matrix for x in row]


Subsequently for each row in the matrix its values are squared. How is this comprehension evaluated?



squared = [[x**2 for x in row] for row in matrix]









share|improve this question

















This question already has an answer here:




  • Understanding nested list comprehension

    2 answers




Given the following expressions:



matrix = [[1,2,3],[4,5,6],[7,8,9]]


A matrix is created, then a list comprehension is executed to create a flat list. The comprehension runs from left to right.



flat = [x for row in matrix for x in row]


Subsequently for each row in the matrix its values are squared. How is this comprehension evaluated?



squared = [[x**2 for x in row] for row in matrix]




This question already has an answer here:




  • Understanding nested list comprehension

    2 answers








python list list-comprehension






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 19:55









timgeb

51.3k126794




51.3k126794










asked Nov 21 '18 at 19:49









dcrearerdcrearer

69421027




69421027




marked as duplicate by roganjosh, juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 20:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by roganjosh, juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 20:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • let me review that post... thanks for the reference

    – dcrearer
    Nov 21 '18 at 19:54



















  • let me review that post... thanks for the reference

    – dcrearer
    Nov 21 '18 at 19:54

















let me review that post... thanks for the reference

– dcrearer
Nov 21 '18 at 19:54





let me review that post... thanks for the reference

– dcrearer
Nov 21 '18 at 19:54












1 Answer
1






active

oldest

votes


















0














The first comprehension is equivalent to:



flat = 
for row in matrix:
for x in row:
flat.append(x)


The second comprehension is equivalent to:



squared = 
for row in matrix:
tmp =
for x in row:
tmp.append(x**2)
squared.append(tmp)


(Except for creating additional variables in the enclosing scope like x, row, tmp.)






share|improve this answer





















  • 1





    thanks for the clear concise sample...

    – dcrearer
    Nov 21 '18 at 19:57











  • @wim right, I overlooked those. The del-ing is probably unnecessary to get the point across.

    – timgeb
    Nov 21 '18 at 20:17


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The first comprehension is equivalent to:



flat = 
for row in matrix:
for x in row:
flat.append(x)


The second comprehension is equivalent to:



squared = 
for row in matrix:
tmp =
for x in row:
tmp.append(x**2)
squared.append(tmp)


(Except for creating additional variables in the enclosing scope like x, row, tmp.)






share|improve this answer





















  • 1





    thanks for the clear concise sample...

    – dcrearer
    Nov 21 '18 at 19:57











  • @wim right, I overlooked those. The del-ing is probably unnecessary to get the point across.

    – timgeb
    Nov 21 '18 at 20:17
















0














The first comprehension is equivalent to:



flat = 
for row in matrix:
for x in row:
flat.append(x)


The second comprehension is equivalent to:



squared = 
for row in matrix:
tmp =
for x in row:
tmp.append(x**2)
squared.append(tmp)


(Except for creating additional variables in the enclosing scope like x, row, tmp.)






share|improve this answer





















  • 1





    thanks for the clear concise sample...

    – dcrearer
    Nov 21 '18 at 19:57











  • @wim right, I overlooked those. The del-ing is probably unnecessary to get the point across.

    – timgeb
    Nov 21 '18 at 20:17














0












0








0







The first comprehension is equivalent to:



flat = 
for row in matrix:
for x in row:
flat.append(x)


The second comprehension is equivalent to:



squared = 
for row in matrix:
tmp =
for x in row:
tmp.append(x**2)
squared.append(tmp)


(Except for creating additional variables in the enclosing scope like x, row, tmp.)






share|improve this answer















The first comprehension is equivalent to:



flat = 
for row in matrix:
for x in row:
flat.append(x)


The second comprehension is equivalent to:



squared = 
for row in matrix:
tmp =
for x in row:
tmp.append(x**2)
squared.append(tmp)


(Except for creating additional variables in the enclosing scope like x, row, tmp.)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 20:17

























answered Nov 21 '18 at 19:53









timgebtimgeb

51.3k126794




51.3k126794








  • 1





    thanks for the clear concise sample...

    – dcrearer
    Nov 21 '18 at 19:57











  • @wim right, I overlooked those. The del-ing is probably unnecessary to get the point across.

    – timgeb
    Nov 21 '18 at 20:17














  • 1





    thanks for the clear concise sample...

    – dcrearer
    Nov 21 '18 at 19:57











  • @wim right, I overlooked those. The del-ing is probably unnecessary to get the point across.

    – timgeb
    Nov 21 '18 at 20:17








1




1





thanks for the clear concise sample...

– dcrearer
Nov 21 '18 at 19:57





thanks for the clear concise sample...

– dcrearer
Nov 21 '18 at 19:57













@wim right, I overlooked those. The del-ing is probably unnecessary to get the point across.

– timgeb
Nov 21 '18 at 20:17





@wim right, I overlooked those. The del-ing is probably unnecessary to get the point across.

– timgeb
Nov 21 '18 at 20:17





Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?