Make every four elements into a single number (element) - python










0















So how to the title description?



So the list:



l=[1,2,3,4,5,6,7,8]


Desired output:



[1234,5678]


So merge the every four packs of elements!



I am thinking about a list comprehension, but it's not a working well (it is, it's below my question)










share|improve this question


























    0















    So how to the title description?



    So the list:



    l=[1,2,3,4,5,6,7,8]


    Desired output:



    [1234,5678]


    So merge the every four packs of elements!



    I am thinking about a list comprehension, but it's not a working well (it is, it's below my question)










    share|improve this question
























      0












      0








      0








      So how to the title description?



      So the list:



      l=[1,2,3,4,5,6,7,8]


      Desired output:



      [1234,5678]


      So merge the every four packs of elements!



      I am thinking about a list comprehension, but it's not a working well (it is, it's below my question)










      share|improve this question














      So how to the title description?



      So the list:



      l=[1,2,3,4,5,6,7,8]


      Desired output:



      [1234,5678]


      So merge the every four packs of elements!



      I am thinking about a list comprehension, but it's not a working well (it is, it's below my question)







      python list merge element






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 0:02









      U9-ForwardU9-Forward

      16.6k51543




      16.6k51543






















          2 Answers
          2






          active

          oldest

          votes


















          2














          You can get the elements from slicing index of the range, we can do that because of the step argument (last one), that make that sequence into an integer (number).



          Note: i am ordering them by rank, so (best on top, and worst at bottom)




          Option 1: list comprehension:




          >>> [int(''.join(map(str,l[i:i+4]))) for i in range(0,len(l),4)]
          [1234, 5678]
          >>>



          Option 2: map:




          >>> list(map(lambda i: int(''.join(map(str,l[i:i+4]))),range(0,len(l),4)))
          [1234, 5678]
          >>>





          share|improve this answer




















          • 1





            Note: Option 1 is the better option, both on readability and performance; any time a map would require the use of lambda that the equivalent listcomp/genexpr can inline, the map will be slower. The use of map in option 1 is fine (because str is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).

            – ShadowRanger
            Nov 13 '18 at 0:13











          • @ShadowRanger Yes sir, That's how i order them :-), i know that :D

            – U9-Forward
            Nov 13 '18 at 0:16


















          0














          Assume the length of l is the multiple of 4 and each element of l is an integer (1 to 9)



          Here is another option without using string.



          [1000*l[4*i]+100*l[4*i+1]+10*l[4*i+2]+l[4*i+3] for i in range(len(l)//4)]





          share|improve this answer

























          • Ouch, Little unreadable, and inefficient..., but thanks for it.

            – U9-Forward
            Nov 13 '18 at 0:19












          • Note: range(len(l)/4) should be range(len(l)//4), use // not /.

            – U9-Forward
            Nov 13 '18 at 0:20











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271871%2fmake-every-four-elements-into-a-single-number-element-python%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You can get the elements from slicing index of the range, we can do that because of the step argument (last one), that make that sequence into an integer (number).



          Note: i am ordering them by rank, so (best on top, and worst at bottom)




          Option 1: list comprehension:




          >>> [int(''.join(map(str,l[i:i+4]))) for i in range(0,len(l),4)]
          [1234, 5678]
          >>>



          Option 2: map:




          >>> list(map(lambda i: int(''.join(map(str,l[i:i+4]))),range(0,len(l),4)))
          [1234, 5678]
          >>>





          share|improve this answer




















          • 1





            Note: Option 1 is the better option, both on readability and performance; any time a map would require the use of lambda that the equivalent listcomp/genexpr can inline, the map will be slower. The use of map in option 1 is fine (because str is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).

            – ShadowRanger
            Nov 13 '18 at 0:13











          • @ShadowRanger Yes sir, That's how i order them :-), i know that :D

            – U9-Forward
            Nov 13 '18 at 0:16















          2














          You can get the elements from slicing index of the range, we can do that because of the step argument (last one), that make that sequence into an integer (number).



          Note: i am ordering them by rank, so (best on top, and worst at bottom)




          Option 1: list comprehension:




          >>> [int(''.join(map(str,l[i:i+4]))) for i in range(0,len(l),4)]
          [1234, 5678]
          >>>



          Option 2: map:




          >>> list(map(lambda i: int(''.join(map(str,l[i:i+4]))),range(0,len(l),4)))
          [1234, 5678]
          >>>





          share|improve this answer




















          • 1





            Note: Option 1 is the better option, both on readability and performance; any time a map would require the use of lambda that the equivalent listcomp/genexpr can inline, the map will be slower. The use of map in option 1 is fine (because str is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).

            – ShadowRanger
            Nov 13 '18 at 0:13











          • @ShadowRanger Yes sir, That's how i order them :-), i know that :D

            – U9-Forward
            Nov 13 '18 at 0:16













          2












          2








          2







          You can get the elements from slicing index of the range, we can do that because of the step argument (last one), that make that sequence into an integer (number).



          Note: i am ordering them by rank, so (best on top, and worst at bottom)




          Option 1: list comprehension:




          >>> [int(''.join(map(str,l[i:i+4]))) for i in range(0,len(l),4)]
          [1234, 5678]
          >>>



          Option 2: map:




          >>> list(map(lambda i: int(''.join(map(str,l[i:i+4]))),range(0,len(l),4)))
          [1234, 5678]
          >>>





          share|improve this answer















          You can get the elements from slicing index of the range, we can do that because of the step argument (last one), that make that sequence into an integer (number).



          Note: i am ordering them by rank, so (best on top, and worst at bottom)




          Option 1: list comprehension:




          >>> [int(''.join(map(str,l[i:i+4]))) for i in range(0,len(l),4)]
          [1234, 5678]
          >>>



          Option 2: map:




          >>> list(map(lambda i: int(''.join(map(str,l[i:i+4]))),range(0,len(l),4)))
          [1234, 5678]
          >>>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 0:15

























          answered Nov 13 '18 at 0:02









          U9-ForwardU9-Forward

          16.6k51543




          16.6k51543







          • 1





            Note: Option 1 is the better option, both on readability and performance; any time a map would require the use of lambda that the equivalent listcomp/genexpr can inline, the map will be slower. The use of map in option 1 is fine (because str is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).

            – ShadowRanger
            Nov 13 '18 at 0:13











          • @ShadowRanger Yes sir, That's how i order them :-), i know that :D

            – U9-Forward
            Nov 13 '18 at 0:16












          • 1





            Note: Option 1 is the better option, both on readability and performance; any time a map would require the use of lambda that the equivalent listcomp/genexpr can inline, the map will be slower. The use of map in option 1 is fine (because str is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).

            – ShadowRanger
            Nov 13 '18 at 0:13











          • @ShadowRanger Yes sir, That's how i order them :-), i know that :D

            – U9-Forward
            Nov 13 '18 at 0:16







          1




          1





          Note: Option 1 is the better option, both on readability and performance; any time a map would require the use of lambda that the equivalent listcomp/genexpr can inline, the map will be slower. The use of map in option 1 is fine (because str is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).

          – ShadowRanger
          Nov 13 '18 at 0:13





          Note: Option 1 is the better option, both on readability and performance; any time a map would require the use of lambda that the equivalent listcomp/genexpr can inline, the map will be slower. The use of map in option 1 is fine (because str is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).

          – ShadowRanger
          Nov 13 '18 at 0:13













          @ShadowRanger Yes sir, That's how i order them :-), i know that :D

          – U9-Forward
          Nov 13 '18 at 0:16





          @ShadowRanger Yes sir, That's how i order them :-), i know that :D

          – U9-Forward
          Nov 13 '18 at 0:16













          0














          Assume the length of l is the multiple of 4 and each element of l is an integer (1 to 9)



          Here is another option without using string.



          [1000*l[4*i]+100*l[4*i+1]+10*l[4*i+2]+l[4*i+3] for i in range(len(l)//4)]





          share|improve this answer

























          • Ouch, Little unreadable, and inefficient..., but thanks for it.

            – U9-Forward
            Nov 13 '18 at 0:19












          • Note: range(len(l)/4) should be range(len(l)//4), use // not /.

            – U9-Forward
            Nov 13 '18 at 0:20
















          0














          Assume the length of l is the multiple of 4 and each element of l is an integer (1 to 9)



          Here is another option without using string.



          [1000*l[4*i]+100*l[4*i+1]+10*l[4*i+2]+l[4*i+3] for i in range(len(l)//4)]





          share|improve this answer

























          • Ouch, Little unreadable, and inefficient..., but thanks for it.

            – U9-Forward
            Nov 13 '18 at 0:19












          • Note: range(len(l)/4) should be range(len(l)//4), use // not /.

            – U9-Forward
            Nov 13 '18 at 0:20














          0












          0








          0







          Assume the length of l is the multiple of 4 and each element of l is an integer (1 to 9)



          Here is another option without using string.



          [1000*l[4*i]+100*l[4*i+1]+10*l[4*i+2]+l[4*i+3] for i in range(len(l)//4)]





          share|improve this answer















          Assume the length of l is the multiple of 4 and each element of l is an integer (1 to 9)



          Here is another option without using string.



          [1000*l[4*i]+100*l[4*i+1]+10*l[4*i+2]+l[4*i+3] for i in range(len(l)//4)]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 0:24

























          answered Nov 13 '18 at 0:17









          Banghua ZhaoBanghua Zhao

          1,2871721




          1,2871721












          • Ouch, Little unreadable, and inefficient..., but thanks for it.

            – U9-Forward
            Nov 13 '18 at 0:19












          • Note: range(len(l)/4) should be range(len(l)//4), use // not /.

            – U9-Forward
            Nov 13 '18 at 0:20


















          • Ouch, Little unreadable, and inefficient..., but thanks for it.

            – U9-Forward
            Nov 13 '18 at 0:19












          • Note: range(len(l)/4) should be range(len(l)//4), use // not /.

            – U9-Forward
            Nov 13 '18 at 0:20

















          Ouch, Little unreadable, and inefficient..., but thanks for it.

          – U9-Forward
          Nov 13 '18 at 0:19






          Ouch, Little unreadable, and inefficient..., but thanks for it.

          – U9-Forward
          Nov 13 '18 at 0:19














          Note: range(len(l)/4) should be range(len(l)//4), use // not /.

          – U9-Forward
          Nov 13 '18 at 0:20






          Note: range(len(l)/4) should be range(len(l)//4), use // not /.

          – U9-Forward
          Nov 13 '18 at 0:20


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271871%2fmake-every-four-elements-into-a-single-number-element-python%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Edmonton

          Crossroads (UK TV series)