Extend multiple elements in one line iteration [duplicate]












4















This question already has an answer here:




  • List comprehension version of “extend” [duplicate]

    1 answer




For



A=[1,2,3]



I would like to get



B=['r1','t1','r2','t2','r3','t3']



I know it is easy to get ['r1','r2','r3'] by



['r'+str(k) for k in A]



How could I get B by one line loop as I showed above?



Many thanks.










share|improve this question













marked as duplicate by Andras Deak, jpp, smci, coldspeed list
Users with the  list badge can single-handedly close list 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 12 at 18:21


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.















  • Possible duplicate of stackoverflow.com/questions/38400096/…, stackoverflow.com/questions/5947137/…, stackoverflow.com/questions/44667519/…
    – Andras Deak
    Nov 12 at 15:12


















4















This question already has an answer here:




  • List comprehension version of “extend” [duplicate]

    1 answer




For



A=[1,2,3]



I would like to get



B=['r1','t1','r2','t2','r3','t3']



I know it is easy to get ['r1','r2','r3'] by



['r'+str(k) for k in A]



How could I get B by one line loop as I showed above?



Many thanks.










share|improve this question













marked as duplicate by Andras Deak, jpp, smci, coldspeed list
Users with the  list badge can single-handedly close list 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 12 at 18:21


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.















  • Possible duplicate of stackoverflow.com/questions/38400096/…, stackoverflow.com/questions/5947137/…, stackoverflow.com/questions/44667519/…
    – Andras Deak
    Nov 12 at 15:12
















4












4








4








This question already has an answer here:




  • List comprehension version of “extend” [duplicate]

    1 answer




For



A=[1,2,3]



I would like to get



B=['r1','t1','r2','t2','r3','t3']



I know it is easy to get ['r1','r2','r3'] by



['r'+str(k) for k in A]



How could I get B by one line loop as I showed above?



Many thanks.










share|improve this question














This question already has an answer here:




  • List comprehension version of “extend” [duplicate]

    1 answer




For



A=[1,2,3]



I would like to get



B=['r1','t1','r2','t2','r3','t3']



I know it is easy to get ['r1','r2','r3'] by



['r'+str(k) for k in A]



How could I get B by one line loop as I showed above?



Many thanks.





This question already has an answer here:




  • List comprehension version of “extend” [duplicate]

    1 answer








python list loops iteration






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 15:08









Xiaojian Chen

425




425




marked as duplicate by Andras Deak, jpp, smci, coldspeed list
Users with the  list badge can single-handedly close list 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 12 at 18:21


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 Andras Deak, jpp, smci, coldspeed list
Users with the  list badge can single-handedly close list 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 12 at 18:21


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.














  • Possible duplicate of stackoverflow.com/questions/38400096/…, stackoverflow.com/questions/5947137/…, stackoverflow.com/questions/44667519/…
    – Andras Deak
    Nov 12 at 15:12




















  • Possible duplicate of stackoverflow.com/questions/38400096/…, stackoverflow.com/questions/5947137/…, stackoverflow.com/questions/44667519/…
    – Andras Deak
    Nov 12 at 15:12


















Possible duplicate of stackoverflow.com/questions/38400096/…, stackoverflow.com/questions/5947137/…, stackoverflow.com/questions/44667519/…
– Andras Deak
Nov 12 at 15:12






Possible duplicate of stackoverflow.com/questions/38400096/…, stackoverflow.com/questions/5947137/…, stackoverflow.com/questions/44667519/…
– Andras Deak
Nov 12 at 15:12














3 Answers
3






active

oldest

votes


















6














You can use a nested list comprehension.



>>> A=[1,2,3]                                                                                                                            
>>> [fmt.format(n) for n in A for fmt in ('r{}', 't{}')]
['r1', 't1', 'r2', 't2', 'r3', 't3']





share|improve this answer

















  • 1




    I like the answer best! It is so clear! Thanks you so much!
    – Xiaojian Chen
    Nov 12 at 15:20



















9














Use a nested list comprehension:



A=[1,2,3]

B = [prefix + str(a) for a in A for prefix in 'rt']





share|improve this answer

















  • 1




    It seems like two loops in a line! It works! Thanks a lot!
    – Xiaojian Chen
    Nov 12 at 15:20



















3














Using itertools.product



import itertools
list(itertools.product(*[[1,2,3],['r','t']]))
Out[20]: [(1, 'r'), (1, 't'), (2, 'r'), (2, 't'), (3, 'r'), (3, 't')]
[y +str(x) for x, y in list(itertools.product(*[[1, 2, 3], ['r', 't']]))]
Out[22]: ['r1', 't1', 'r2', 't2', 'r3', 't3']





share|improve this answer























  • I think you probably want y + str(x) but otherwise, good use of itertools
    – Woody1193
    Nov 12 at 15:12






  • 3




    Is there some people given downvote for all answer here ? u'd better leave a reason
    – W-B
    Nov 12 at 15:12












  • @Woody1193 thank you updated
    – W-B
    Nov 12 at 15:13


















3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














You can use a nested list comprehension.



>>> A=[1,2,3]                                                                                                                            
>>> [fmt.format(n) for n in A for fmt in ('r{}', 't{}')]
['r1', 't1', 'r2', 't2', 'r3', 't3']





share|improve this answer

















  • 1




    I like the answer best! It is so clear! Thanks you so much!
    – Xiaojian Chen
    Nov 12 at 15:20
















6














You can use a nested list comprehension.



>>> A=[1,2,3]                                                                                                                            
>>> [fmt.format(n) for n in A for fmt in ('r{}', 't{}')]
['r1', 't1', 'r2', 't2', 'r3', 't3']





share|improve this answer

















  • 1




    I like the answer best! It is so clear! Thanks you so much!
    – Xiaojian Chen
    Nov 12 at 15:20














6












6








6






You can use a nested list comprehension.



>>> A=[1,2,3]                                                                                                                            
>>> [fmt.format(n) for n in A for fmt in ('r{}', 't{}')]
['r1', 't1', 'r2', 't2', 'r3', 't3']





share|improve this answer












You can use a nested list comprehension.



>>> A=[1,2,3]                                                                                                                            
>>> [fmt.format(n) for n in A for fmt in ('r{}', 't{}')]
['r1', 't1', 'r2', 't2', 'r3', 't3']






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 15:10









timgeb

48.8k116390




48.8k116390








  • 1




    I like the answer best! It is so clear! Thanks you so much!
    – Xiaojian Chen
    Nov 12 at 15:20














  • 1




    I like the answer best! It is so clear! Thanks you so much!
    – Xiaojian Chen
    Nov 12 at 15:20








1




1




I like the answer best! It is so clear! Thanks you so much!
– Xiaojian Chen
Nov 12 at 15:20




I like the answer best! It is so clear! Thanks you so much!
– Xiaojian Chen
Nov 12 at 15:20













9














Use a nested list comprehension:



A=[1,2,3]

B = [prefix + str(a) for a in A for prefix in 'rt']





share|improve this answer

















  • 1




    It seems like two loops in a line! It works! Thanks a lot!
    – Xiaojian Chen
    Nov 12 at 15:20
















9














Use a nested list comprehension:



A=[1,2,3]

B = [prefix + str(a) for a in A for prefix in 'rt']





share|improve this answer

















  • 1




    It seems like two loops in a line! It works! Thanks a lot!
    – Xiaojian Chen
    Nov 12 at 15:20














9












9








9






Use a nested list comprehension:



A=[1,2,3]

B = [prefix + str(a) for a in A for prefix in 'rt']





share|improve this answer












Use a nested list comprehension:



A=[1,2,3]

B = [prefix + str(a) for a in A for prefix in 'rt']






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 15:09









Daniel Mesejo

12.1k1924




12.1k1924








  • 1




    It seems like two loops in a line! It works! Thanks a lot!
    – Xiaojian Chen
    Nov 12 at 15:20














  • 1




    It seems like two loops in a line! It works! Thanks a lot!
    – Xiaojian Chen
    Nov 12 at 15:20








1




1




It seems like two loops in a line! It works! Thanks a lot!
– Xiaojian Chen
Nov 12 at 15:20




It seems like two loops in a line! It works! Thanks a lot!
– Xiaojian Chen
Nov 12 at 15:20











3














Using itertools.product



import itertools
list(itertools.product(*[[1,2,3],['r','t']]))
Out[20]: [(1, 'r'), (1, 't'), (2, 'r'), (2, 't'), (3, 'r'), (3, 't')]
[y +str(x) for x, y in list(itertools.product(*[[1, 2, 3], ['r', 't']]))]
Out[22]: ['r1', 't1', 'r2', 't2', 'r3', 't3']





share|improve this answer























  • I think you probably want y + str(x) but otherwise, good use of itertools
    – Woody1193
    Nov 12 at 15:12






  • 3




    Is there some people given downvote for all answer here ? u'd better leave a reason
    – W-B
    Nov 12 at 15:12












  • @Woody1193 thank you updated
    – W-B
    Nov 12 at 15:13
















3














Using itertools.product



import itertools
list(itertools.product(*[[1,2,3],['r','t']]))
Out[20]: [(1, 'r'), (1, 't'), (2, 'r'), (2, 't'), (3, 'r'), (3, 't')]
[y +str(x) for x, y in list(itertools.product(*[[1, 2, 3], ['r', 't']]))]
Out[22]: ['r1', 't1', 'r2', 't2', 'r3', 't3']





share|improve this answer























  • I think you probably want y + str(x) but otherwise, good use of itertools
    – Woody1193
    Nov 12 at 15:12






  • 3




    Is there some people given downvote for all answer here ? u'd better leave a reason
    – W-B
    Nov 12 at 15:12












  • @Woody1193 thank you updated
    – W-B
    Nov 12 at 15:13














3












3








3






Using itertools.product



import itertools
list(itertools.product(*[[1,2,3],['r','t']]))
Out[20]: [(1, 'r'), (1, 't'), (2, 'r'), (2, 't'), (3, 'r'), (3, 't')]
[y +str(x) for x, y in list(itertools.product(*[[1, 2, 3], ['r', 't']]))]
Out[22]: ['r1', 't1', 'r2', 't2', 'r3', 't3']





share|improve this answer














Using itertools.product



import itertools
list(itertools.product(*[[1,2,3],['r','t']]))
Out[20]: [(1, 'r'), (1, 't'), (2, 'r'), (2, 't'), (3, 'r'), (3, 't')]
[y +str(x) for x, y in list(itertools.product(*[[1, 2, 3], ['r', 't']]))]
Out[22]: ['r1', 't1', 'r2', 't2', 'r3', 't3']






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 15:13

























answered Nov 12 at 15:11









W-B

99.8k73163




99.8k73163












  • I think you probably want y + str(x) but otherwise, good use of itertools
    – Woody1193
    Nov 12 at 15:12






  • 3




    Is there some people given downvote for all answer here ? u'd better leave a reason
    – W-B
    Nov 12 at 15:12












  • @Woody1193 thank you updated
    – W-B
    Nov 12 at 15:13


















  • I think you probably want y + str(x) but otherwise, good use of itertools
    – Woody1193
    Nov 12 at 15:12






  • 3




    Is there some people given downvote for all answer here ? u'd better leave a reason
    – W-B
    Nov 12 at 15:12












  • @Woody1193 thank you updated
    – W-B
    Nov 12 at 15:13
















I think you probably want y + str(x) but otherwise, good use of itertools
– Woody1193
Nov 12 at 15:12




I think you probably want y + str(x) but otherwise, good use of itertools
– Woody1193
Nov 12 at 15:12




3




3




Is there some people given downvote for all answer here ? u'd better leave a reason
– W-B
Nov 12 at 15:12






Is there some people given downvote for all answer here ? u'd better leave a reason
– W-B
Nov 12 at 15:12














@Woody1193 thank you updated
– W-B
Nov 12 at 15:13




@Woody1193 thank you updated
– W-B
Nov 12 at 15:13



Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python