Get a list (without nested list) directly from a list comprehension [duplicate]
This question already has an answer here:
Interleave multiple lists of the same length in Python
7 answers
I want to return a list with the interleaved elements of two lists directly from the list comprehension - without use a next step to flaten the result. Is it possible?
alist1_temp=[1, 4,2]
alist2_temp=[3, 7,4]
t=[[x,y] for x,y in zip(alist1_temp, alist2_temp)]
Return [[1, 3], [4, 7],[2, 4]]
But how can I get directly from the list-comprehension [1, 3, 4, 7, 2, 4]
?
python list list-comprehension
marked as duplicate by shash678, ShadowRanger
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();
}
);
});
});
Jan 6 at 15:29
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 |
This question already has an answer here:
Interleave multiple lists of the same length in Python
7 answers
I want to return a list with the interleaved elements of two lists directly from the list comprehension - without use a next step to flaten the result. Is it possible?
alist1_temp=[1, 4,2]
alist2_temp=[3, 7,4]
t=[[x,y] for x,y in zip(alist1_temp, alist2_temp)]
Return [[1, 3], [4, 7],[2, 4]]
But how can I get directly from the list-comprehension [1, 3, 4, 7, 2, 4]
?
python list list-comprehension
marked as duplicate by shash678, ShadowRanger
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();
}
);
});
});
Jan 6 at 15:29
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 |
This question already has an answer here:
Interleave multiple lists of the same length in Python
7 answers
I want to return a list with the interleaved elements of two lists directly from the list comprehension - without use a next step to flaten the result. Is it possible?
alist1_temp=[1, 4,2]
alist2_temp=[3, 7,4]
t=[[x,y] for x,y in zip(alist1_temp, alist2_temp)]
Return [[1, 3], [4, 7],[2, 4]]
But how can I get directly from the list-comprehension [1, 3, 4, 7, 2, 4]
?
python list list-comprehension
This question already has an answer here:
Interleave multiple lists of the same length in Python
7 answers
I want to return a list with the interleaved elements of two lists directly from the list comprehension - without use a next step to flaten the result. Is it possible?
alist1_temp=[1, 4,2]
alist2_temp=[3, 7,4]
t=[[x,y] for x,y in zip(alist1_temp, alist2_temp)]
Return [[1, 3], [4, 7],[2, 4]]
But how can I get directly from the list-comprehension [1, 3, 4, 7, 2, 4]
?
This question already has an answer here:
Interleave multiple lists of the same length in Python
7 answers
python list list-comprehension
python list list-comprehension
edited Jan 6 at 14:48
jonrsharpe
77k11102208
77k11102208
asked Jan 6 at 14:36
Pedro PintoPedro Pinto
704
704
marked as duplicate by shash678, ShadowRanger
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();
}
);
});
});
Jan 6 at 15:29
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 shash678, ShadowRanger
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();
}
);
});
});
Jan 6 at 15:29
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 |
add a comment |
3 Answers
3
active
oldest
votes
Try this with just zip
to get it in that order that you want:
[i for j in zip(alist1_temp, alist2_temp) for i in j]
if you don't mind the order just do:
alist1_temp + alist2_temp
or get it with itertools.chain
thanks to @buran:
import itertools
list(itertools.chain(alist1_temp, alist2_temp))
with the chain - why not justlist(itertools.chain(alist1_temp, alist2_temp))
? it's not comprehension, but I am not sure comprehension is strict requirement. And anyway the order of elements in the result is different.
– buran
Jan 6 at 14:48
@buran Right, surely you are right
– Mehrdad Pedramfar
Jan 6 at 14:49
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
Jan 6 at 14:50
add a comment |
If you like numpy way of doing this, you could use this!
np.vstack((alist1_temp,alist2_temp)).flatten('F')
or you can flatten your list comprehension as well
np.array(t).flatten()
add a comment |
As you specified that you want get it from from the list-comprehension:
alist1_temp=[1,4,2]
alist2_temp=[3,7,4]
L = len(alist1_temp)+len(alist2_temp)
t = [alist2_temp[i//2] if i%2 else alist1_temp[i//2] for i in range(L)]
print(t) #prints [1, 3, 4, 7, 2, 4]
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this with just zip
to get it in that order that you want:
[i for j in zip(alist1_temp, alist2_temp) for i in j]
if you don't mind the order just do:
alist1_temp + alist2_temp
or get it with itertools.chain
thanks to @buran:
import itertools
list(itertools.chain(alist1_temp, alist2_temp))
with the chain - why not justlist(itertools.chain(alist1_temp, alist2_temp))
? it's not comprehension, but I am not sure comprehension is strict requirement. And anyway the order of elements in the result is different.
– buran
Jan 6 at 14:48
@buran Right, surely you are right
– Mehrdad Pedramfar
Jan 6 at 14:49
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
Jan 6 at 14:50
add a comment |
Try this with just zip
to get it in that order that you want:
[i for j in zip(alist1_temp, alist2_temp) for i in j]
if you don't mind the order just do:
alist1_temp + alist2_temp
or get it with itertools.chain
thanks to @buran:
import itertools
list(itertools.chain(alist1_temp, alist2_temp))
with the chain - why not justlist(itertools.chain(alist1_temp, alist2_temp))
? it's not comprehension, but I am not sure comprehension is strict requirement. And anyway the order of elements in the result is different.
– buran
Jan 6 at 14:48
@buran Right, surely you are right
– Mehrdad Pedramfar
Jan 6 at 14:49
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
Jan 6 at 14:50
add a comment |
Try this with just zip
to get it in that order that you want:
[i for j in zip(alist1_temp, alist2_temp) for i in j]
if you don't mind the order just do:
alist1_temp + alist2_temp
or get it with itertools.chain
thanks to @buran:
import itertools
list(itertools.chain(alist1_temp, alist2_temp))
Try this with just zip
to get it in that order that you want:
[i for j in zip(alist1_temp, alist2_temp) for i in j]
if you don't mind the order just do:
alist1_temp + alist2_temp
or get it with itertools.chain
thanks to @buran:
import itertools
list(itertools.chain(alist1_temp, alist2_temp))
edited Jan 6 at 14:50
answered Jan 6 at 14:38
Mehrdad PedramfarMehrdad Pedramfar
4,89211237
4,89211237
with the chain - why not justlist(itertools.chain(alist1_temp, alist2_temp))
? it's not comprehension, but I am not sure comprehension is strict requirement. And anyway the order of elements in the result is different.
– buran
Jan 6 at 14:48
@buran Right, surely you are right
– Mehrdad Pedramfar
Jan 6 at 14:49
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
Jan 6 at 14:50
add a comment |
with the chain - why not justlist(itertools.chain(alist1_temp, alist2_temp))
? it's not comprehension, but I am not sure comprehension is strict requirement. And anyway the order of elements in the result is different.
– buran
Jan 6 at 14:48
@buran Right, surely you are right
– Mehrdad Pedramfar
Jan 6 at 14:49
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
Jan 6 at 14:50
with the chain - why not just
list(itertools.chain(alist1_temp, alist2_temp))
? it's not comprehension, but I am not sure comprehension is strict requirement. And anyway the order of elements in the result is different.– buran
Jan 6 at 14:48
with the chain - why not just
list(itertools.chain(alist1_temp, alist2_temp))
? it's not comprehension, but I am not sure comprehension is strict requirement. And anyway the order of elements in the result is different.– buran
Jan 6 at 14:48
@buran Right, surely you are right
– Mehrdad Pedramfar
Jan 6 at 14:49
@buran Right, surely you are right
– Mehrdad Pedramfar
Jan 6 at 14:49
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
Jan 6 at 14:50
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
Jan 6 at 14:50
add a comment |
If you like numpy way of doing this, you could use this!
np.vstack((alist1_temp,alist2_temp)).flatten('F')
or you can flatten your list comprehension as well
np.array(t).flatten()
add a comment |
If you like numpy way of doing this, you could use this!
np.vstack((alist1_temp,alist2_temp)).flatten('F')
or you can flatten your list comprehension as well
np.array(t).flatten()
add a comment |
If you like numpy way of doing this, you could use this!
np.vstack((alist1_temp,alist2_temp)).flatten('F')
or you can flatten your list comprehension as well
np.array(t).flatten()
If you like numpy way of doing this, you could use this!
np.vstack((alist1_temp,alist2_temp)).flatten('F')
or you can flatten your list comprehension as well
np.array(t).flatten()
answered Jan 6 at 14:54
AI_LearningAI_Learning
2,6451729
2,6451729
add a comment |
add a comment |
As you specified that you want get it from from the list-comprehension:
alist1_temp=[1,4,2]
alist2_temp=[3,7,4]
L = len(alist1_temp)+len(alist2_temp)
t = [alist2_temp[i//2] if i%2 else alist1_temp[i//2] for i in range(L)]
print(t) #prints [1, 3, 4, 7, 2, 4]
add a comment |
As you specified that you want get it from from the list-comprehension:
alist1_temp=[1,4,2]
alist2_temp=[3,7,4]
L = len(alist1_temp)+len(alist2_temp)
t = [alist2_temp[i//2] if i%2 else alist1_temp[i//2] for i in range(L)]
print(t) #prints [1, 3, 4, 7, 2, 4]
add a comment |
As you specified that you want get it from from the list-comprehension:
alist1_temp=[1,4,2]
alist2_temp=[3,7,4]
L = len(alist1_temp)+len(alist2_temp)
t = [alist2_temp[i//2] if i%2 else alist1_temp[i//2] for i in range(L)]
print(t) #prints [1, 3, 4, 7, 2, 4]
As you specified that you want get it from from the list-comprehension:
alist1_temp=[1,4,2]
alist2_temp=[3,7,4]
L = len(alist1_temp)+len(alist2_temp)
t = [alist2_temp[i//2] if i%2 else alist1_temp[i//2] for i in range(L)]
print(t) #prints [1, 3, 4, 7, 2, 4]
answered Jan 6 at 15:14
DaweoDaweo
2813
2813
add a comment |
add a comment |