execution order of expressions in a list comprehensions [duplicate]
up vote
0
down vote
favorite
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]
python list list-comprehension
marked as duplicate by roganjosh, juanpa.arrivillaga
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 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.
add a comment |
up vote
0
down vote
favorite
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]
python list list-comprehension
marked as duplicate by roganjosh, juanpa.arrivillaga
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 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 at 19:54
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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]
python list list-comprehension
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
python list list-comprehension
edited Nov 21 at 19:55
timgeb
47.6k116288
47.6k116288
asked Nov 21 at 19:49
dcrearer
6492726
6492726
marked as duplicate by roganjosh, juanpa.arrivillaga
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 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
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 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 at 19:54
add a comment |
let me review that post... thanks for the reference
– dcrearer
Nov 21 at 19:54
let me review that post... thanks for the reference
– dcrearer
Nov 21 at 19:54
let me review that post... thanks for the reference
– dcrearer
Nov 21 at 19:54
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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
.)
1
thanks for the clear concise sample...
– dcrearer
Nov 21 at 19:57
@wim right, I overlooked those. Thedel
-ing is probably unnecessary to get the point across.
– timgeb
Nov 21 at 20:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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
.)
1
thanks for the clear concise sample...
– dcrearer
Nov 21 at 19:57
@wim right, I overlooked those. Thedel
-ing is probably unnecessary to get the point across.
– timgeb
Nov 21 at 20:17
add a comment |
up vote
0
down vote
accepted
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
.)
1
thanks for the clear concise sample...
– dcrearer
Nov 21 at 19:57
@wim right, I overlooked those. Thedel
-ing is probably unnecessary to get the point across.
– timgeb
Nov 21 at 20:17
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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
.)
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
.)
edited Nov 21 at 20:17
answered Nov 21 at 19:53
timgeb
47.6k116288
47.6k116288
1
thanks for the clear concise sample...
– dcrearer
Nov 21 at 19:57
@wim right, I overlooked those. Thedel
-ing is probably unnecessary to get the point across.
– timgeb
Nov 21 at 20:17
add a comment |
1
thanks for the clear concise sample...
– dcrearer
Nov 21 at 19:57
@wim right, I overlooked those. Thedel
-ing is probably unnecessary to get the point across.
– timgeb
Nov 21 at 20:17
1
1
thanks for the clear concise sample...
– dcrearer
Nov 21 at 19:57
thanks for the clear concise sample...
– dcrearer
Nov 21 at 19:57
@wim right, I overlooked those. The
del
-ing is probably unnecessary to get the point across.– timgeb
Nov 21 at 20:17
@wim right, I overlooked those. The
del
-ing is probably unnecessary to get the point across.– timgeb
Nov 21 at 20:17
add a comment |
let me review that post... thanks for the reference
– dcrearer
Nov 21 at 19:54