Get a list (without nested list) directly from a list comprehension [duplicate]












6















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]?










share|improve this question















marked as duplicate by shash678, ShadowRanger 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();
}
);
});
});
1 hour 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.




















    6















    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]?










    share|improve this question















    marked as duplicate by shash678, ShadowRanger 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();
    }
    );
    });
    });
    1 hour 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.


















      6












      6








      6








      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]?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      jonrsharpe

      76.8k11102208




      76.8k11102208










      asked 1 hour ago









      Pedro Pinto

      704




      704




      marked as duplicate by shash678, ShadowRanger 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();
      }
      );
      });
      });
      1 hour 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 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();
      }
      );
      });
      });
      1 hour 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.


























          3 Answers
          3






          active

          oldest

          votes


















          5














          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))





          share|improve this answer























          • 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
            1 hour ago












          • @buran Right, surely you are right
            – Mehrdad Pedramfar
            1 hour ago










          • @buran Edited, thanks for your comment
            – Mehrdad Pedramfar
            1 hour ago



















          1














          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()





          share|improve this answer





























            1














            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]





            share|improve this answer




























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              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))





              share|improve this answer























              • 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
                1 hour ago












              • @buran Right, surely you are right
                – Mehrdad Pedramfar
                1 hour ago










              • @buran Edited, thanks for your comment
                – Mehrdad Pedramfar
                1 hour ago
















              5














              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))





              share|improve this answer























              • 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
                1 hour ago












              • @buran Right, surely you are right
                – Mehrdad Pedramfar
                1 hour ago










              • @buran Edited, thanks for your comment
                – Mehrdad Pedramfar
                1 hour ago














              5












              5








              5






              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))





              share|improve this answer














              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))






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 1 hour ago

























              answered 1 hour ago









              Mehrdad Pedramfar

              4,78211237




              4,78211237












              • 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
                1 hour ago












              • @buran Right, surely you are right
                – Mehrdad Pedramfar
                1 hour ago










              • @buran Edited, thanks for your comment
                – Mehrdad Pedramfar
                1 hour 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
                1 hour ago












              • @buran Right, surely you are right
                – Mehrdad Pedramfar
                1 hour ago










              • @buran Edited, thanks for your comment
                – Mehrdad Pedramfar
                1 hour 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
              1 hour 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
              1 hour ago














              @buran Right, surely you are right
              – Mehrdad Pedramfar
              1 hour ago




              @buran Right, surely you are right
              – Mehrdad Pedramfar
              1 hour ago












              @buran Edited, thanks for your comment
              – Mehrdad Pedramfar
              1 hour ago




              @buran Edited, thanks for your comment
              – Mehrdad Pedramfar
              1 hour ago













              1














              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()





              share|improve this answer


























                1














                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()





                share|improve this answer
























                  1












                  1








                  1






                  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()





                  share|improve this answer












                  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()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  AI_Learning

                  2,4691628




                  2,4691628























                      1














                      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]





                      share|improve this answer


























                        1














                        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]





                        share|improve this answer
























                          1












                          1








                          1






                          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]





                          share|improve this answer












                          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]






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 1 hour ago









                          Daweo

                          1562




                          1562















                              Popular posts from this blog

                              How to ignore python UserWarning in pytest?

                              The difference between “忍受不住” and “忍受不了”

                              Service Worker FechtEvent.respondWith response is null on iOS 12.1 Safari