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();
}
);
});
});
yesterday


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();
    }
    );
    });
    });
    yesterday


    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 yesterday









      jonrsharpe

      76.8k11102208




      76.8k11102208










      asked yesterday









      Pedro PintoPedro 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();
      }
      );
      });
      });
      yesterday


      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();
      }
      );
      });
      });
      yesterday


      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
            yesterday












          • @buran Right, surely you are right
            – Mehrdad Pedramfar
            yesterday










          • @buran Edited, thanks for your comment
            – Mehrdad Pedramfar
            yesterday



















          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
                yesterday












              • @buran Right, surely you are right
                – Mehrdad Pedramfar
                yesterday










              • @buran Edited, thanks for your comment
                – Mehrdad Pedramfar
                yesterday
















              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
                yesterday












              • @buran Right, surely you are right
                – Mehrdad Pedramfar
                yesterday










              • @buran Edited, thanks for your comment
                – Mehrdad Pedramfar
                yesterday














              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 yesterday

























              answered yesterday









              Mehrdad PedramfarMehrdad 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
                yesterday












              • @buran Right, surely you are right
                – Mehrdad Pedramfar
                yesterday










              • @buran Edited, thanks for your comment
                – Mehrdad Pedramfar
                yesterday


















              • 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
                yesterday












              • @buran Right, surely you are right
                – Mehrdad Pedramfar
                yesterday










              • @buran Edited, thanks for your comment
                – Mehrdad Pedramfar
                yesterday
















              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
              yesterday






              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
              yesterday














              @buran Right, surely you are right
              – Mehrdad Pedramfar
              yesterday




              @buran Right, surely you are right
              – Mehrdad Pedramfar
              yesterday












              @buran Edited, thanks for your comment
              – Mehrdad Pedramfar
              yesterday




              @buran Edited, thanks for your comment
              – Mehrdad Pedramfar
              yesterday













              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 yesterday









                  AI_LearningAI_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 yesterday









                          DaweoDaweo

                          1662




                          1662















                              Popular posts from this blog

                              An IMO inspired problem

                              Management

                              Has there ever been an instance of an active nuclear power plant within or near a war zone?