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();
}
);
});
});
2 days ago
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();
}
);
});
});
2 days ago
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 2 days ago
jonrsharpe
76.9k11102208
76.9k11102208
asked 2 days ago
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();
}
);
});
});
2 days ago
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();
}
);
});
});
2 days ago
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
2 days ago
@buran Right, surely you are right
– Mehrdad Pedramfar
2 days ago
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
2 days ago
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
2 days ago
@buran Right, surely you are right
– Mehrdad Pedramfar
2 days ago
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
2 days ago
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
2 days ago
@buran Right, surely you are right
– Mehrdad Pedramfar
2 days ago
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
2 days ago
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 2 days ago
answered 2 days ago
Mehrdad PedramfarMehrdad Pedramfar
4,84711237
4,84711237
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
2 days ago
@buran Right, surely you are right
– Mehrdad Pedramfar
2 days ago
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
2 days ago
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
2 days ago
@buran Right, surely you are right
– Mehrdad Pedramfar
2 days ago
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
2 days ago
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
2 days ago
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
2 days ago
@buran Right, surely you are right
– Mehrdad Pedramfar
2 days ago
@buran Right, surely you are right
– Mehrdad Pedramfar
2 days ago
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
2 days ago
@buran Edited, thanks for your comment
– Mehrdad Pedramfar
2 days ago
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 2 days ago
AI_LearningAI_Learning
2,4791628
2,4791628
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 2 days ago
DaweoDaweo
1763
1763
add a comment |
add a comment |