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




















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


















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


























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



















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
















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














              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 Jan 6 at 14:50

























              answered Jan 6 at 14:38









              Mehrdad PedramfarMehrdad Pedramfar

              4,89211237




              4,89211237













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













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













              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 Jan 6 at 14:54









                  AI_LearningAI_Learning

                  2,6451729




                  2,6451729























                      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 Jan 6 at 15:14









                          DaweoDaweo

                          2813




                          2813















                              Popular posts from this blog

                              Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                              ComboBox Display Member on multiple fields

                              Is it possible to collect Nectar points via Trainline?