List with duplicated values and suffix












37















I have a list, a:



a = ['a','b','c']


and need to duplicate some values with the suffix _ind added this way (order is important):



['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


I tried:



b = [[x, x + '_ind'] for x in a]
c = [item for sublist in b for item in sublist]
print (c)
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


I think my solution is a bit over-complicated. Is there some better, more pythonic solution?










share|improve this question




















  • 6





    For the record, there's nothing wrong with this solution.

    – coldspeed
    Jul 15 '17 at 20:09


















37















I have a list, a:



a = ['a','b','c']


and need to duplicate some values with the suffix _ind added this way (order is important):



['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


I tried:



b = [[x, x + '_ind'] for x in a]
c = [item for sublist in b for item in sublist]
print (c)
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


I think my solution is a bit over-complicated. Is there some better, more pythonic solution?










share|improve this question




















  • 6





    For the record, there's nothing wrong with this solution.

    – coldspeed
    Jul 15 '17 at 20:09
















37












37








37


9






I have a list, a:



a = ['a','b','c']


and need to duplicate some values with the suffix _ind added this way (order is important):



['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


I tried:



b = [[x, x + '_ind'] for x in a]
c = [item for sublist in b for item in sublist]
print (c)
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


I think my solution is a bit over-complicated. Is there some better, more pythonic solution?










share|improve this question
















I have a list, a:



a = ['a','b','c']


and need to duplicate some values with the suffix _ind added this way (order is important):



['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


I tried:



b = [[x, x + '_ind'] for x in a]
c = [item for sublist in b for item in sublist]
print (c)
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


I think my solution is a bit over-complicated. Is there some better, more pythonic solution?







python list list-comprehension suffix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 15 '17 at 20:46









Christian Dean

14.9k52655




14.9k52655










asked Jul 15 '17 at 19:45









jezraeljezrael

329k23271350




329k23271350








  • 6





    For the record, there's nothing wrong with this solution.

    – coldspeed
    Jul 15 '17 at 20:09
















  • 6





    For the record, there's nothing wrong with this solution.

    – coldspeed
    Jul 15 '17 at 20:09










6




6





For the record, there's nothing wrong with this solution.

– coldspeed
Jul 15 '17 at 20:09







For the record, there's nothing wrong with this solution.

– coldspeed
Jul 15 '17 at 20:09














6 Answers
6






active

oldest

votes


















52














You could make it a generator:



def mygen(lst):
for item in lst:
yield item
yield item + '_ind'

>>> a = ['a','b','c']
>>> list(mygen(a))
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


You could also do it with itertools.product, itertools.starmap or itertools.chain or nested comprehensions but in most cases I would prefer a simple to understand, custom generator-function.





With python3.3, you can also use yield from—generator delegation—to make this elegant solution just a bit more concise:



def mygen(lst):
for item in lst:
yield from (item, item + '_ind')





share|improve this answer





















  • 5





    Might I suggest a list comprehension version? list([((yield x), (yield (x + '_ind'))) for x in a]) ; ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

    – coldspeed
    Jul 15 '17 at 20:12








  • 16





    @cᴏʟᴅsᴘᴇᴇᴅ Wtf... never seen such "inline" yield. Played with it some more, this works as well: list([(yield from (x, x + '_ind')) for x in a]).

    – Stefan Pochmann
    Jul 15 '17 at 20:15






  • 2





    @StefanPochmann Killer. Want to post an answer with it? If not, I'll edit mine. :p

    – coldspeed
    Jul 15 '17 at 20:16








  • 3





    @StefanPochmann It's py3k only. They added yield support for list comps to allow generators to be created.

    – coldspeed
    Jul 15 '17 at 20:19








  • 3





    @idjaw Oh wait, just saw the red underlines on my IDE too. xD

    – coldspeed
    Jul 15 '17 at 20:27



















26














It can be shortened a little bit by moving the options to the inner for loop in the list comprehension:



a = ['a','b','c']

[item for x in a for item in (x, x + '_ind')]
# ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']





share|improve this answer































    14














    Another alternative with splicing (Python2.x, 3.x):



    In [642]: result = [None] * len(a) * 2

    In [643]: result[::2], result[1::2] = a, map(lambda x: x + '_ind', a)

    In [644]: result
    Out[644]: ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']





    share|improve this answer





















    • 1





      "Python 3.x only" - Actually it's only Python 3.3 and greater. See here

      – Christian Dean
      Jul 15 '17 at 21:18






    • 2





      @Bergi Yes, because a list comp with yield returns a generator.

      – coldspeed
      Jul 16 '17 at 7:10






    • 1





      This option is most useful if you want to obfuscate your Python code.

      – Sven Marnach
      Aug 11 '17 at 15:01






    • 1





      @cᴏʟᴅsᴘᴇᴇᴅ No, for code golf Psidom's answer wins. :)

      – Sven Marnach
      Aug 11 '17 at 19:17






    • 1





      @coldspeed - thanks.

      – jezrael
      Jan 8 at 10:34



















    5














    You can use itertools.chain():



    import itertools

    l = ['a','b','c']

    new_list = list(itertools.chain.from_iterable([[i, i+"_ind"] for i in l]))

    print new_list


    Output:



    ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']





    share|improve this answer





















    • 4





      I would use chain.from_iterable. That way you don't need the unpacking with *

      – MSeifert
      Jul 15 '17 at 19:59








    • 1





      Alt: list(itertools.chain(*zip(a, [x + '_ind' for x in a])) ) or list(itertools.chain(*zip(a, map(lambda x: x + '_ind', a))))

      – coldspeed
      Jul 15 '17 at 20:03








    • 1





      @cᴏʟᴅsᴘᴇᴇᴅ Agreed, but again, chain.from_iterable would IMHO be a bit cleaner :) list(chain.from_iterable(zip(a, [i+'_ind' for i in a]))). Not that it is particularly important.

      – miradulo
      Jul 15 '17 at 20:08



















    3














    Before list comprehensions and generators were invented/became widespread, people used to think much simpler1:



    >>> a = ['a', 'b', 'c']
    >>> b =
    >>> for x in a: b.extend([x, x+'_ind'])
    ...
    >>> b
    ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']




    * I don't mean that those constructs/tools are evil, just wanted to point out that there is a simple solution.






    share|improve this answer
























    • As far as I know were comprehensions and generators invented (implemented) to make these "constructs and tools" simpler (and definitely a lot faster). But I guess that's a quite opinion-based statement so YMMV.

      – MSeifert
      Jul 17 '17 at 16:56





















    3














    Since you asked for "simple", I thought I'd throw this in (albeit, maybe not the pythonic way):



    for i in mylist: 
    mylist1.append(i);
    mylist1.append(i + '_ind');





    share|improve this answer

























      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%2f45122154%2flist-with-duplicated-values-and-suffix%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      52














      You could make it a generator:



      def mygen(lst):
      for item in lst:
      yield item
      yield item + '_ind'

      >>> a = ['a','b','c']
      >>> list(mygen(a))
      ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


      You could also do it with itertools.product, itertools.starmap or itertools.chain or nested comprehensions but in most cases I would prefer a simple to understand, custom generator-function.





      With python3.3, you can also use yield from—generator delegation—to make this elegant solution just a bit more concise:



      def mygen(lst):
      for item in lst:
      yield from (item, item + '_ind')





      share|improve this answer





















      • 5





        Might I suggest a list comprehension version? list([((yield x), (yield (x + '_ind'))) for x in a]) ; ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

        – coldspeed
        Jul 15 '17 at 20:12








      • 16





        @cᴏʟᴅsᴘᴇᴇᴅ Wtf... never seen such "inline" yield. Played with it some more, this works as well: list([(yield from (x, x + '_ind')) for x in a]).

        – Stefan Pochmann
        Jul 15 '17 at 20:15






      • 2





        @StefanPochmann Killer. Want to post an answer with it? If not, I'll edit mine. :p

        – coldspeed
        Jul 15 '17 at 20:16








      • 3





        @StefanPochmann It's py3k only. They added yield support for list comps to allow generators to be created.

        – coldspeed
        Jul 15 '17 at 20:19








      • 3





        @idjaw Oh wait, just saw the red underlines on my IDE too. xD

        – coldspeed
        Jul 15 '17 at 20:27
















      52














      You could make it a generator:



      def mygen(lst):
      for item in lst:
      yield item
      yield item + '_ind'

      >>> a = ['a','b','c']
      >>> list(mygen(a))
      ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


      You could also do it with itertools.product, itertools.starmap or itertools.chain or nested comprehensions but in most cases I would prefer a simple to understand, custom generator-function.





      With python3.3, you can also use yield from—generator delegation—to make this elegant solution just a bit more concise:



      def mygen(lst):
      for item in lst:
      yield from (item, item + '_ind')





      share|improve this answer





















      • 5





        Might I suggest a list comprehension version? list([((yield x), (yield (x + '_ind'))) for x in a]) ; ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

        – coldspeed
        Jul 15 '17 at 20:12








      • 16





        @cᴏʟᴅsᴘᴇᴇᴅ Wtf... never seen such "inline" yield. Played with it some more, this works as well: list([(yield from (x, x + '_ind')) for x in a]).

        – Stefan Pochmann
        Jul 15 '17 at 20:15






      • 2





        @StefanPochmann Killer. Want to post an answer with it? If not, I'll edit mine. :p

        – coldspeed
        Jul 15 '17 at 20:16








      • 3





        @StefanPochmann It's py3k only. They added yield support for list comps to allow generators to be created.

        – coldspeed
        Jul 15 '17 at 20:19








      • 3





        @idjaw Oh wait, just saw the red underlines on my IDE too. xD

        – coldspeed
        Jul 15 '17 at 20:27














      52












      52








      52







      You could make it a generator:



      def mygen(lst):
      for item in lst:
      yield item
      yield item + '_ind'

      >>> a = ['a','b','c']
      >>> list(mygen(a))
      ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


      You could also do it with itertools.product, itertools.starmap or itertools.chain or nested comprehensions but in most cases I would prefer a simple to understand, custom generator-function.





      With python3.3, you can also use yield from—generator delegation—to make this elegant solution just a bit more concise:



      def mygen(lst):
      for item in lst:
      yield from (item, item + '_ind')





      share|improve this answer















      You could make it a generator:



      def mygen(lst):
      for item in lst:
      yield item
      yield item + '_ind'

      >>> a = ['a','b','c']
      >>> list(mygen(a))
      ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']


      You could also do it with itertools.product, itertools.starmap or itertools.chain or nested comprehensions but in most cases I would prefer a simple to understand, custom generator-function.





      With python3.3, you can also use yield from—generator delegation—to make this elegant solution just a bit more concise:



      def mygen(lst):
      for item in lst:
      yield from (item, item + '_ind')






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 14 '18 at 2:11









      coldspeed

      127k23128214




      127k23128214










      answered Jul 15 '17 at 19:51









      MSeifertMSeifert

      74.8k17145178




      74.8k17145178








      • 5





        Might I suggest a list comprehension version? list([((yield x), (yield (x + '_ind'))) for x in a]) ; ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

        – coldspeed
        Jul 15 '17 at 20:12








      • 16





        @cᴏʟᴅsᴘᴇᴇᴅ Wtf... never seen such "inline" yield. Played with it some more, this works as well: list([(yield from (x, x + '_ind')) for x in a]).

        – Stefan Pochmann
        Jul 15 '17 at 20:15






      • 2





        @StefanPochmann Killer. Want to post an answer with it? If not, I'll edit mine. :p

        – coldspeed
        Jul 15 '17 at 20:16








      • 3





        @StefanPochmann It's py3k only. They added yield support for list comps to allow generators to be created.

        – coldspeed
        Jul 15 '17 at 20:19








      • 3





        @idjaw Oh wait, just saw the red underlines on my IDE too. xD

        – coldspeed
        Jul 15 '17 at 20:27














      • 5





        Might I suggest a list comprehension version? list([((yield x), (yield (x + '_ind'))) for x in a]) ; ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

        – coldspeed
        Jul 15 '17 at 20:12








      • 16





        @cᴏʟᴅsᴘᴇᴇᴅ Wtf... never seen such "inline" yield. Played with it some more, this works as well: list([(yield from (x, x + '_ind')) for x in a]).

        – Stefan Pochmann
        Jul 15 '17 at 20:15






      • 2





        @StefanPochmann Killer. Want to post an answer with it? If not, I'll edit mine. :p

        – coldspeed
        Jul 15 '17 at 20:16








      • 3





        @StefanPochmann It's py3k only. They added yield support for list comps to allow generators to be created.

        – coldspeed
        Jul 15 '17 at 20:19








      • 3





        @idjaw Oh wait, just saw the red underlines on my IDE too. xD

        – coldspeed
        Jul 15 '17 at 20:27








      5




      5





      Might I suggest a list comprehension version? list([((yield x), (yield (x + '_ind'))) for x in a]) ; ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

      – coldspeed
      Jul 15 '17 at 20:12







      Might I suggest a list comprehension version? list([((yield x), (yield (x + '_ind'))) for x in a]) ; ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

      – coldspeed
      Jul 15 '17 at 20:12






      16




      16





      @cᴏʟᴅsᴘᴇᴇᴅ Wtf... never seen such "inline" yield. Played with it some more, this works as well: list([(yield from (x, x + '_ind')) for x in a]).

      – Stefan Pochmann
      Jul 15 '17 at 20:15





      @cᴏʟᴅsᴘᴇᴇᴅ Wtf... never seen such "inline" yield. Played with it some more, this works as well: list([(yield from (x, x + '_ind')) for x in a]).

      – Stefan Pochmann
      Jul 15 '17 at 20:15




      2




      2





      @StefanPochmann Killer. Want to post an answer with it? If not, I'll edit mine. :p

      – coldspeed
      Jul 15 '17 at 20:16







      @StefanPochmann Killer. Want to post an answer with it? If not, I'll edit mine. :p

      – coldspeed
      Jul 15 '17 at 20:16






      3




      3





      @StefanPochmann It's py3k only. They added yield support for list comps to allow generators to be created.

      – coldspeed
      Jul 15 '17 at 20:19







      @StefanPochmann It's py3k only. They added yield support for list comps to allow generators to be created.

      – coldspeed
      Jul 15 '17 at 20:19






      3




      3





      @idjaw Oh wait, just saw the red underlines on my IDE too. xD

      – coldspeed
      Jul 15 '17 at 20:27





      @idjaw Oh wait, just saw the red underlines on my IDE too. xD

      – coldspeed
      Jul 15 '17 at 20:27













      26














      It can be shortened a little bit by moving the options to the inner for loop in the list comprehension:



      a = ['a','b','c']

      [item for x in a for item in (x, x + '_ind')]
      # ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']





      share|improve this answer




























        26














        It can be shortened a little bit by moving the options to the inner for loop in the list comprehension:



        a = ['a','b','c']

        [item for x in a for item in (x, x + '_ind')]
        # ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']





        share|improve this answer


























          26












          26








          26







          It can be shortened a little bit by moving the options to the inner for loop in the list comprehension:



          a = ['a','b','c']

          [item for x in a for item in (x, x + '_ind')]
          # ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']





          share|improve this answer













          It can be shortened a little bit by moving the options to the inner for loop in the list comprehension:



          a = ['a','b','c']

          [item for x in a for item in (x, x + '_ind')]
          # ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 15 '17 at 19:50









          PsidomPsidom

          123k1285127




          123k1285127























              14














              Another alternative with splicing (Python2.x, 3.x):



              In [642]: result = [None] * len(a) * 2

              In [643]: result[::2], result[1::2] = a, map(lambda x: x + '_ind', a)

              In [644]: result
              Out[644]: ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']





              share|improve this answer





















              • 1





                "Python 3.x only" - Actually it's only Python 3.3 and greater. See here

                – Christian Dean
                Jul 15 '17 at 21:18






              • 2





                @Bergi Yes, because a list comp with yield returns a generator.

                – coldspeed
                Jul 16 '17 at 7:10






              • 1





                This option is most useful if you want to obfuscate your Python code.

                – Sven Marnach
                Aug 11 '17 at 15:01






              • 1





                @cᴏʟᴅsᴘᴇᴇᴅ No, for code golf Psidom's answer wins. :)

                – Sven Marnach
                Aug 11 '17 at 19:17






              • 1





                @coldspeed - thanks.

                – jezrael
                Jan 8 at 10:34
















              14














              Another alternative with splicing (Python2.x, 3.x):



              In [642]: result = [None] * len(a) * 2

              In [643]: result[::2], result[1::2] = a, map(lambda x: x + '_ind', a)

              In [644]: result
              Out[644]: ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']





              share|improve this answer





















              • 1





                "Python 3.x only" - Actually it's only Python 3.3 and greater. See here

                – Christian Dean
                Jul 15 '17 at 21:18






              • 2





                @Bergi Yes, because a list comp with yield returns a generator.

                – coldspeed
                Jul 16 '17 at 7:10






              • 1





                This option is most useful if you want to obfuscate your Python code.

                – Sven Marnach
                Aug 11 '17 at 15:01






              • 1





                @cᴏʟᴅsᴘᴇᴇᴅ No, for code golf Psidom's answer wins. :)

                – Sven Marnach
                Aug 11 '17 at 19:17






              • 1





                @coldspeed - thanks.

                – jezrael
                Jan 8 at 10:34














              14












              14








              14







              Another alternative with splicing (Python2.x, 3.x):



              In [642]: result = [None] * len(a) * 2

              In [643]: result[::2], result[1::2] = a, map(lambda x: x + '_ind', a)

              In [644]: result
              Out[644]: ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']





              share|improve this answer















              Another alternative with splicing (Python2.x, 3.x):



              In [642]: result = [None] * len(a) * 2

              In [643]: result[::2], result[1::2] = a, map(lambda x: x + '_ind', a)

              In [644]: result
              Out[644]: ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Oct 13 '17 at 7:16

























              answered Jul 15 '17 at 20:00









              coldspeedcoldspeed

              127k23128214




              127k23128214








              • 1





                "Python 3.x only" - Actually it's only Python 3.3 and greater. See here

                – Christian Dean
                Jul 15 '17 at 21:18






              • 2





                @Bergi Yes, because a list comp with yield returns a generator.

                – coldspeed
                Jul 16 '17 at 7:10






              • 1





                This option is most useful if you want to obfuscate your Python code.

                – Sven Marnach
                Aug 11 '17 at 15:01






              • 1





                @cᴏʟᴅsᴘᴇᴇᴅ No, for code golf Psidom's answer wins. :)

                – Sven Marnach
                Aug 11 '17 at 19:17






              • 1





                @coldspeed - thanks.

                – jezrael
                Jan 8 at 10:34














              • 1





                "Python 3.x only" - Actually it's only Python 3.3 and greater. See here

                – Christian Dean
                Jul 15 '17 at 21:18






              • 2





                @Bergi Yes, because a list comp with yield returns a generator.

                – coldspeed
                Jul 16 '17 at 7:10






              • 1





                This option is most useful if you want to obfuscate your Python code.

                – Sven Marnach
                Aug 11 '17 at 15:01






              • 1





                @cᴏʟᴅsᴘᴇᴇᴅ No, for code golf Psidom's answer wins. :)

                – Sven Marnach
                Aug 11 '17 at 19:17






              • 1





                @coldspeed - thanks.

                – jezrael
                Jan 8 at 10:34








              1




              1





              "Python 3.x only" - Actually it's only Python 3.3 and greater. See here

              – Christian Dean
              Jul 15 '17 at 21:18





              "Python 3.x only" - Actually it's only Python 3.3 and greater. See here

              – Christian Dean
              Jul 15 '17 at 21:18




              2




              2





              @Bergi Yes, because a list comp with yield returns a generator.

              – coldspeed
              Jul 16 '17 at 7:10





              @Bergi Yes, because a list comp with yield returns a generator.

              – coldspeed
              Jul 16 '17 at 7:10




              1




              1





              This option is most useful if you want to obfuscate your Python code.

              – Sven Marnach
              Aug 11 '17 at 15:01





              This option is most useful if you want to obfuscate your Python code.

              – Sven Marnach
              Aug 11 '17 at 15:01




              1




              1





              @cᴏʟᴅsᴘᴇᴇᴅ No, for code golf Psidom's answer wins. :)

              – Sven Marnach
              Aug 11 '17 at 19:17





              @cᴏʟᴅsᴘᴇᴇᴅ No, for code golf Psidom's answer wins. :)

              – Sven Marnach
              Aug 11 '17 at 19:17




              1




              1





              @coldspeed - thanks.

              – jezrael
              Jan 8 at 10:34





              @coldspeed - thanks.

              – jezrael
              Jan 8 at 10:34











              5














              You can use itertools.chain():



              import itertools

              l = ['a','b','c']

              new_list = list(itertools.chain.from_iterable([[i, i+"_ind"] for i in l]))

              print new_list


              Output:



              ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']





              share|improve this answer





















              • 4





                I would use chain.from_iterable. That way you don't need the unpacking with *

                – MSeifert
                Jul 15 '17 at 19:59








              • 1





                Alt: list(itertools.chain(*zip(a, [x + '_ind' for x in a])) ) or list(itertools.chain(*zip(a, map(lambda x: x + '_ind', a))))

                – coldspeed
                Jul 15 '17 at 20:03








              • 1





                @cᴏʟᴅsᴘᴇᴇᴅ Agreed, but again, chain.from_iterable would IMHO be a bit cleaner :) list(chain.from_iterable(zip(a, [i+'_ind' for i in a]))). Not that it is particularly important.

                – miradulo
                Jul 15 '17 at 20:08
















              5














              You can use itertools.chain():



              import itertools

              l = ['a','b','c']

              new_list = list(itertools.chain.from_iterable([[i, i+"_ind"] for i in l]))

              print new_list


              Output:



              ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']





              share|improve this answer





















              • 4





                I would use chain.from_iterable. That way you don't need the unpacking with *

                – MSeifert
                Jul 15 '17 at 19:59








              • 1





                Alt: list(itertools.chain(*zip(a, [x + '_ind' for x in a])) ) or list(itertools.chain(*zip(a, map(lambda x: x + '_ind', a))))

                – coldspeed
                Jul 15 '17 at 20:03








              • 1





                @cᴏʟᴅsᴘᴇᴇᴅ Agreed, but again, chain.from_iterable would IMHO be a bit cleaner :) list(chain.from_iterable(zip(a, [i+'_ind' for i in a]))). Not that it is particularly important.

                – miradulo
                Jul 15 '17 at 20:08














              5












              5








              5







              You can use itertools.chain():



              import itertools

              l = ['a','b','c']

              new_list = list(itertools.chain.from_iterable([[i, i+"_ind"] for i in l]))

              print new_list


              Output:



              ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']





              share|improve this answer















              You can use itertools.chain():



              import itertools

              l = ['a','b','c']

              new_list = list(itertools.chain.from_iterable([[i, i+"_ind"] for i in l]))

              print new_list


              Output:



              ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 16 '17 at 14:05

























              answered Jul 15 '17 at 19:58









              Ajax1234Ajax1234

              41.1k42853




              41.1k42853








              • 4





                I would use chain.from_iterable. That way you don't need the unpacking with *

                – MSeifert
                Jul 15 '17 at 19:59








              • 1





                Alt: list(itertools.chain(*zip(a, [x + '_ind' for x in a])) ) or list(itertools.chain(*zip(a, map(lambda x: x + '_ind', a))))

                – coldspeed
                Jul 15 '17 at 20:03








              • 1





                @cᴏʟᴅsᴘᴇᴇᴅ Agreed, but again, chain.from_iterable would IMHO be a bit cleaner :) list(chain.from_iterable(zip(a, [i+'_ind' for i in a]))). Not that it is particularly important.

                – miradulo
                Jul 15 '17 at 20:08














              • 4





                I would use chain.from_iterable. That way you don't need the unpacking with *

                – MSeifert
                Jul 15 '17 at 19:59








              • 1





                Alt: list(itertools.chain(*zip(a, [x + '_ind' for x in a])) ) or list(itertools.chain(*zip(a, map(lambda x: x + '_ind', a))))

                – coldspeed
                Jul 15 '17 at 20:03








              • 1





                @cᴏʟᴅsᴘᴇᴇᴅ Agreed, but again, chain.from_iterable would IMHO be a bit cleaner :) list(chain.from_iterable(zip(a, [i+'_ind' for i in a]))). Not that it is particularly important.

                – miradulo
                Jul 15 '17 at 20:08








              4




              4





              I would use chain.from_iterable. That way you don't need the unpacking with *

              – MSeifert
              Jul 15 '17 at 19:59







              I would use chain.from_iterable. That way you don't need the unpacking with *

              – MSeifert
              Jul 15 '17 at 19:59






              1




              1





              Alt: list(itertools.chain(*zip(a, [x + '_ind' for x in a])) ) or list(itertools.chain(*zip(a, map(lambda x: x + '_ind', a))))

              – coldspeed
              Jul 15 '17 at 20:03







              Alt: list(itertools.chain(*zip(a, [x + '_ind' for x in a])) ) or list(itertools.chain(*zip(a, map(lambda x: x + '_ind', a))))

              – coldspeed
              Jul 15 '17 at 20:03






              1




              1





              @cᴏʟᴅsᴘᴇᴇᴅ Agreed, but again, chain.from_iterable would IMHO be a bit cleaner :) list(chain.from_iterable(zip(a, [i+'_ind' for i in a]))). Not that it is particularly important.

              – miradulo
              Jul 15 '17 at 20:08





              @cᴏʟᴅsᴘᴇᴇᴅ Agreed, but again, chain.from_iterable would IMHO be a bit cleaner :) list(chain.from_iterable(zip(a, [i+'_ind' for i in a]))). Not that it is particularly important.

              – miradulo
              Jul 15 '17 at 20:08











              3














              Before list comprehensions and generators were invented/became widespread, people used to think much simpler1:



              >>> a = ['a', 'b', 'c']
              >>> b =
              >>> for x in a: b.extend([x, x+'_ind'])
              ...
              >>> b
              ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']




              * I don't mean that those constructs/tools are evil, just wanted to point out that there is a simple solution.






              share|improve this answer
























              • As far as I know were comprehensions and generators invented (implemented) to make these "constructs and tools" simpler (and definitely a lot faster). But I guess that's a quite opinion-based statement so YMMV.

                – MSeifert
                Jul 17 '17 at 16:56


















              3














              Before list comprehensions and generators were invented/became widespread, people used to think much simpler1:



              >>> a = ['a', 'b', 'c']
              >>> b =
              >>> for x in a: b.extend([x, x+'_ind'])
              ...
              >>> b
              ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']




              * I don't mean that those constructs/tools are evil, just wanted to point out that there is a simple solution.






              share|improve this answer
























              • As far as I know were comprehensions and generators invented (implemented) to make these "constructs and tools" simpler (and definitely a lot faster). But I guess that's a quite opinion-based statement so YMMV.

                – MSeifert
                Jul 17 '17 at 16:56
















              3












              3








              3







              Before list comprehensions and generators were invented/became widespread, people used to think much simpler1:



              >>> a = ['a', 'b', 'c']
              >>> b =
              >>> for x in a: b.extend([x, x+'_ind'])
              ...
              >>> b
              ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']




              * I don't mean that those constructs/tools are evil, just wanted to point out that there is a simple solution.






              share|improve this answer













              Before list comprehensions and generators were invented/became widespread, people used to think much simpler1:



              >>> a = ['a', 'b', 'c']
              >>> b =
              >>> for x in a: b.extend([x, x+'_ind'])
              ...
              >>> b
              ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']




              * I don't mean that those constructs/tools are evil, just wanted to point out that there is a simple solution.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 17 '17 at 16:30









              LeonLeon

              20.8k23270




              20.8k23270













              • As far as I know were comprehensions and generators invented (implemented) to make these "constructs and tools" simpler (and definitely a lot faster). But I guess that's a quite opinion-based statement so YMMV.

                – MSeifert
                Jul 17 '17 at 16:56





















              • As far as I know were comprehensions and generators invented (implemented) to make these "constructs and tools" simpler (and definitely a lot faster). But I guess that's a quite opinion-based statement so YMMV.

                – MSeifert
                Jul 17 '17 at 16:56



















              As far as I know were comprehensions and generators invented (implemented) to make these "constructs and tools" simpler (and definitely a lot faster). But I guess that's a quite opinion-based statement so YMMV.

              – MSeifert
              Jul 17 '17 at 16:56







              As far as I know were comprehensions and generators invented (implemented) to make these "constructs and tools" simpler (and definitely a lot faster). But I guess that's a quite opinion-based statement so YMMV.

              – MSeifert
              Jul 17 '17 at 16:56













              3














              Since you asked for "simple", I thought I'd throw this in (albeit, maybe not the pythonic way):



              for i in mylist: 
              mylist1.append(i);
              mylist1.append(i + '_ind');





              share|improve this answer






























                3














                Since you asked for "simple", I thought I'd throw this in (albeit, maybe not the pythonic way):



                for i in mylist: 
                mylist1.append(i);
                mylist1.append(i + '_ind');





                share|improve this answer




























                  3












                  3








                  3







                  Since you asked for "simple", I thought I'd throw this in (albeit, maybe not the pythonic way):



                  for i in mylist: 
                  mylist1.append(i);
                  mylist1.append(i + '_ind');





                  share|improve this answer















                  Since you asked for "simple", I thought I'd throw this in (albeit, maybe not the pythonic way):



                  for i in mylist: 
                  mylist1.append(i);
                  mylist1.append(i + '_ind');






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 8 '18 at 20:12









                  today

                  10.7k21837




                  10.7k21837










                  answered Oct 6 '17 at 9:39









                  EladianEladian

                  492316




                  492316






























                      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%2f45122154%2flist-with-duplicated-values-and-suffix%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

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python