Python replace in list











up vote
1
down vote

favorite












I have a list with different email formatting, which I want to unify all in one.
There are 2 types of email:



name.surname@dom
name.surname.extern@dom


My program allows the user to input emails, and to not have to enter "@dom" all the time (it's always the same one), what I've done is allowing the user to write name.surname or name.surname.e and then the script replaces those usernames with @dom or .extern@dom
The problem rises when I have all the mails in different formats stores in a list, and I want them to be filled to standards, so that if I have



["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]


it all ends up looking like this



["john.doe@dom", "john2.doe@dom", "john3.doe.extern@dom","john4.doe.extern@dom"]


I have tried with list comprehensions, but all I got was three concatenations:



["%s.xtern@dom" % x for x in mails if x[-2:] == ".e"] +
["%s@dom" %x for x in mails if "@dom not in mails" and x[-2:] != ".e"] +
[x for x in mails if "@dom" in x]


I'm sure there's a better way that does not require me to do 3 list comprehensions and that does not require me to do



for i,v in enumerate(mails):
if "@dom" not in v:
mails[i] = "%s@dom" % v
etc.









share|improve this question


























    up vote
    1
    down vote

    favorite












    I have a list with different email formatting, which I want to unify all in one.
    There are 2 types of email:



    name.surname@dom
    name.surname.extern@dom


    My program allows the user to input emails, and to not have to enter "@dom" all the time (it's always the same one), what I've done is allowing the user to write name.surname or name.surname.e and then the script replaces those usernames with @dom or .extern@dom
    The problem rises when I have all the mails in different formats stores in a list, and I want them to be filled to standards, so that if I have



    ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]


    it all ends up looking like this



    ["john.doe@dom", "john2.doe@dom", "john3.doe.extern@dom","john4.doe.extern@dom"]


    I have tried with list comprehensions, but all I got was three concatenations:



    ["%s.xtern@dom" % x for x in mails if x[-2:] == ".e"] +
    ["%s@dom" %x for x in mails if "@dom not in mails" and x[-2:] != ".e"] +
    [x for x in mails if "@dom" in x]


    I'm sure there's a better way that does not require me to do 3 list comprehensions and that does not require me to do



    for i,v in enumerate(mails):
    if "@dom" not in v:
    mails[i] = "%s@dom" % v
    etc.









    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a list with different email formatting, which I want to unify all in one.
      There are 2 types of email:



      name.surname@dom
      name.surname.extern@dom


      My program allows the user to input emails, and to not have to enter "@dom" all the time (it's always the same one), what I've done is allowing the user to write name.surname or name.surname.e and then the script replaces those usernames with @dom or .extern@dom
      The problem rises when I have all the mails in different formats stores in a list, and I want them to be filled to standards, so that if I have



      ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]


      it all ends up looking like this



      ["john.doe@dom", "john2.doe@dom", "john3.doe.extern@dom","john4.doe.extern@dom"]


      I have tried with list comprehensions, but all I got was three concatenations:



      ["%s.xtern@dom" % x for x in mails if x[-2:] == ".e"] +
      ["%s@dom" %x for x in mails if "@dom not in mails" and x[-2:] != ".e"] +
      [x for x in mails if "@dom" in x]


      I'm sure there's a better way that does not require me to do 3 list comprehensions and that does not require me to do



      for i,v in enumerate(mails):
      if "@dom" not in v:
      mails[i] = "%s@dom" % v
      etc.









      share|improve this question













      I have a list with different email formatting, which I want to unify all in one.
      There are 2 types of email:



      name.surname@dom
      name.surname.extern@dom


      My program allows the user to input emails, and to not have to enter "@dom" all the time (it's always the same one), what I've done is allowing the user to write name.surname or name.surname.e and then the script replaces those usernames with @dom or .extern@dom
      The problem rises when I have all the mails in different formats stores in a list, and I want them to be filled to standards, so that if I have



      ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]


      it all ends up looking like this



      ["john.doe@dom", "john2.doe@dom", "john3.doe.extern@dom","john4.doe.extern@dom"]


      I have tried with list comprehensions, but all I got was three concatenations:



      ["%s.xtern@dom" % x for x in mails if x[-2:] == ".e"] +
      ["%s@dom" %x for x in mails if "@dom not in mails" and x[-2:] != ".e"] +
      [x for x in mails if "@dom" in x]


      I'm sure there's a better way that does not require me to do 3 list comprehensions and that does not require me to do



      for i,v in enumerate(mails):
      if "@dom" not in v:
      mails[i] = "%s@dom" % v
      etc.






      python list list-comprehension






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Hamperfait

      125110




      125110
























          5 Answers
          5






          active

          oldest

          votes

















          up vote
          1
          down vote













          You can use a string's endswith() method to determine what you need to do with the input:



          mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

          final_mails =

          for mail in mails:
          if mail.endswith("@dom"):
          # Use as-is if it ends with @dom.
          final_mails.append(mail)
          elif mail.endswith(".e"):
          # Replace to extern@dom if it ends with .e
          final_mails.append(mail.replace(".e", ".extern@dom"))
          else:
          # Add @dom on all other cases
          final_mails.append("{}@dom".format(mail))

          print final_mails

          # Result: ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


          It might need more thorough checks to not accept things like @dom right in the middle of the name and whatnot. Hope that helps you out though!



          Edit:
          Just for fun, if you insist on a list comprehension:



          mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

          final_mails = ["{}@dom".format((mail.replace(".e", ".extern@dom")
          if mail.endswith(".e") else mail).rstrip("@dom"))
          for mail in mails]

          print final_mails

          # Result: ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


          Personally I find list comprehensions are best when they are short and readable, so I would stick with the first option.






          share|improve this answer






























            up vote
            0
            down vote













            Option without listcomprehensions:



            maillist = ["john.doe@dom", "john2.doe",    "john3.doe.e","john4.doe.extern@dom"]

            for i in range(len(maillist)):
            if "@" not in maillist[i]:
            if maillist[i][-2:]== ".e":
            maillist[i] += ("xtern@dom")
            else:
            maillist[i] += "@dom"





            share|improve this answer




























              up vote
              0
              down vote













              @Green Cell was faster than me and his answer seems to be correct. Here is a list comprehension that does the same thing :



              mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

              print mails

              mails = [mail if mail.endswith("@dom") else mail.replace(".e", ".extern@dom") if mail.endswith(".e") else "{}@dom".format(mail) for mail in mails]

              print mails


              Which outputs :



              ['john.doe@dom', 'john2.doe', 'john3.doe.e', 'john4.doe.extern@dom']
              ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


              Hope this helps.






              share|improve this answer




























                up vote
                0
                down vote













                if you want a one-liner, combine list comprehension with multiple if/else statements:



                first_list = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                second_list = [email + 'xtern@dom' if email.endswith(".e") else
                email if email.endswith("@dom") else "{}@dom".format(email)
                for email in first_list]

                print second_list


                Gives:



                ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']





                share|improve this answer






























                  up vote
                  0
                  down vote













                  I came with a different solution in the end: Declaring a help function.



                  def mailfy(mail):
                  if mail.endswith(".c"):
                  return mail[:-2] + "@dom"
                  ...

                  mails = [mailfy(x) for x in mails]





                  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',
                    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%2f53203425%2fpython-replace-in-list%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest
































                    5 Answers
                    5






                    active

                    oldest

                    votes








                    5 Answers
                    5






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes








                    up vote
                    1
                    down vote













                    You can use a string's endswith() method to determine what you need to do with the input:



                    mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                    final_mails =

                    for mail in mails:
                    if mail.endswith("@dom"):
                    # Use as-is if it ends with @dom.
                    final_mails.append(mail)
                    elif mail.endswith(".e"):
                    # Replace to extern@dom if it ends with .e
                    final_mails.append(mail.replace(".e", ".extern@dom"))
                    else:
                    # Add @dom on all other cases
                    final_mails.append("{}@dom".format(mail))

                    print final_mails

                    # Result: ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


                    It might need more thorough checks to not accept things like @dom right in the middle of the name and whatnot. Hope that helps you out though!



                    Edit:
                    Just for fun, if you insist on a list comprehension:



                    mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                    final_mails = ["{}@dom".format((mail.replace(".e", ".extern@dom")
                    if mail.endswith(".e") else mail).rstrip("@dom"))
                    for mail in mails]

                    print final_mails

                    # Result: ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


                    Personally I find list comprehensions are best when they are short and readable, so I would stick with the first option.






                    share|improve this answer



























                      up vote
                      1
                      down vote













                      You can use a string's endswith() method to determine what you need to do with the input:



                      mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                      final_mails =

                      for mail in mails:
                      if mail.endswith("@dom"):
                      # Use as-is if it ends with @dom.
                      final_mails.append(mail)
                      elif mail.endswith(".e"):
                      # Replace to extern@dom if it ends with .e
                      final_mails.append(mail.replace(".e", ".extern@dom"))
                      else:
                      # Add @dom on all other cases
                      final_mails.append("{}@dom".format(mail))

                      print final_mails

                      # Result: ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


                      It might need more thorough checks to not accept things like @dom right in the middle of the name and whatnot. Hope that helps you out though!



                      Edit:
                      Just for fun, if you insist on a list comprehension:



                      mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                      final_mails = ["{}@dom".format((mail.replace(".e", ".extern@dom")
                      if mail.endswith(".e") else mail).rstrip("@dom"))
                      for mail in mails]

                      print final_mails

                      # Result: ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


                      Personally I find list comprehensions are best when they are short and readable, so I would stick with the first option.






                      share|improve this answer

























                        up vote
                        1
                        down vote










                        up vote
                        1
                        down vote









                        You can use a string's endswith() method to determine what you need to do with the input:



                        mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                        final_mails =

                        for mail in mails:
                        if mail.endswith("@dom"):
                        # Use as-is if it ends with @dom.
                        final_mails.append(mail)
                        elif mail.endswith(".e"):
                        # Replace to extern@dom if it ends with .e
                        final_mails.append(mail.replace(".e", ".extern@dom"))
                        else:
                        # Add @dom on all other cases
                        final_mails.append("{}@dom".format(mail))

                        print final_mails

                        # Result: ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


                        It might need more thorough checks to not accept things like @dom right in the middle of the name and whatnot. Hope that helps you out though!



                        Edit:
                        Just for fun, if you insist on a list comprehension:



                        mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                        final_mails = ["{}@dom".format((mail.replace(".e", ".extern@dom")
                        if mail.endswith(".e") else mail).rstrip("@dom"))
                        for mail in mails]

                        print final_mails

                        # Result: ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


                        Personally I find list comprehensions are best when they are short and readable, so I would stick with the first option.






                        share|improve this answer














                        You can use a string's endswith() method to determine what you need to do with the input:



                        mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                        final_mails =

                        for mail in mails:
                        if mail.endswith("@dom"):
                        # Use as-is if it ends with @dom.
                        final_mails.append(mail)
                        elif mail.endswith(".e"):
                        # Replace to extern@dom if it ends with .e
                        final_mails.append(mail.replace(".e", ".extern@dom"))
                        else:
                        # Add @dom on all other cases
                        final_mails.append("{}@dom".format(mail))

                        print final_mails

                        # Result: ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


                        It might need more thorough checks to not accept things like @dom right in the middle of the name and whatnot. Hope that helps you out though!



                        Edit:
                        Just for fun, if you insist on a list comprehension:



                        mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                        final_mails = ["{}@dom".format((mail.replace(".e", ".extern@dom")
                        if mail.endswith(".e") else mail).rstrip("@dom"))
                        for mail in mails]

                        print final_mails

                        # Result: ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


                        Personally I find list comprehensions are best when they are short and readable, so I would stick with the first option.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited yesterday

























                        answered yesterday









                        Green Cell

                        1,527823




                        1,527823
























                            up vote
                            0
                            down vote













                            Option without listcomprehensions:



                            maillist = ["john.doe@dom", "john2.doe",    "john3.doe.e","john4.doe.extern@dom"]

                            for i in range(len(maillist)):
                            if "@" not in maillist[i]:
                            if maillist[i][-2:]== ".e":
                            maillist[i] += ("xtern@dom")
                            else:
                            maillist[i] += "@dom"





                            share|improve this answer

























                              up vote
                              0
                              down vote













                              Option without listcomprehensions:



                              maillist = ["john.doe@dom", "john2.doe",    "john3.doe.e","john4.doe.extern@dom"]

                              for i in range(len(maillist)):
                              if "@" not in maillist[i]:
                              if maillist[i][-2:]== ".e":
                              maillist[i] += ("xtern@dom")
                              else:
                              maillist[i] += "@dom"





                              share|improve this answer























                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                Option without listcomprehensions:



                                maillist = ["john.doe@dom", "john2.doe",    "john3.doe.e","john4.doe.extern@dom"]

                                for i in range(len(maillist)):
                                if "@" not in maillist[i]:
                                if maillist[i][-2:]== ".e":
                                maillist[i] += ("xtern@dom")
                                else:
                                maillist[i] += "@dom"





                                share|improve this answer












                                Option without listcomprehensions:



                                maillist = ["john.doe@dom", "john2.doe",    "john3.doe.e","john4.doe.extern@dom"]

                                for i in range(len(maillist)):
                                if "@" not in maillist[i]:
                                if maillist[i][-2:]== ".e":
                                maillist[i] += ("xtern@dom")
                                else:
                                maillist[i] += "@dom"






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered yesterday









                                petruz

                                431311




                                431311






















                                    up vote
                                    0
                                    down vote













                                    @Green Cell was faster than me and his answer seems to be correct. Here is a list comprehension that does the same thing :



                                    mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                                    print mails

                                    mails = [mail if mail.endswith("@dom") else mail.replace(".e", ".extern@dom") if mail.endswith(".e") else "{}@dom".format(mail) for mail in mails]

                                    print mails


                                    Which outputs :



                                    ['john.doe@dom', 'john2.doe', 'john3.doe.e', 'john4.doe.extern@dom']
                                    ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


                                    Hope this helps.






                                    share|improve this answer

























                                      up vote
                                      0
                                      down vote













                                      @Green Cell was faster than me and his answer seems to be correct. Here is a list comprehension that does the same thing :



                                      mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                                      print mails

                                      mails = [mail if mail.endswith("@dom") else mail.replace(".e", ".extern@dom") if mail.endswith(".e") else "{}@dom".format(mail) for mail in mails]

                                      print mails


                                      Which outputs :



                                      ['john.doe@dom', 'john2.doe', 'john3.doe.e', 'john4.doe.extern@dom']
                                      ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


                                      Hope this helps.






                                      share|improve this answer























                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        @Green Cell was faster than me and his answer seems to be correct. Here is a list comprehension that does the same thing :



                                        mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                                        print mails

                                        mails = [mail if mail.endswith("@dom") else mail.replace(".e", ".extern@dom") if mail.endswith(".e") else "{}@dom".format(mail) for mail in mails]

                                        print mails


                                        Which outputs :



                                        ['john.doe@dom', 'john2.doe', 'john3.doe.e', 'john4.doe.extern@dom']
                                        ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


                                        Hope this helps.






                                        share|improve this answer












                                        @Green Cell was faster than me and his answer seems to be correct. Here is a list comprehension that does the same thing :



                                        mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                                        print mails

                                        mails = [mail if mail.endswith("@dom") else mail.replace(".e", ".extern@dom") if mail.endswith(".e") else "{}@dom".format(mail) for mail in mails]

                                        print mails


                                        Which outputs :



                                        ['john.doe@dom', 'john2.doe', 'john3.doe.e', 'john4.doe.extern@dom']
                                        ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']


                                        Hope this helps.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered yesterday









                                        Alan B.

                                        45110




                                        45110






















                                            up vote
                                            0
                                            down vote













                                            if you want a one-liner, combine list comprehension with multiple if/else statements:



                                            first_list = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                                            second_list = [email + 'xtern@dom' if email.endswith(".e") else
                                            email if email.endswith("@dom") else "{}@dom".format(email)
                                            for email in first_list]

                                            print second_list


                                            Gives:



                                            ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']





                                            share|improve this answer



























                                              up vote
                                              0
                                              down vote













                                              if you want a one-liner, combine list comprehension with multiple if/else statements:



                                              first_list = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                                              second_list = [email + 'xtern@dom' if email.endswith(".e") else
                                              email if email.endswith("@dom") else "{}@dom".format(email)
                                              for email in first_list]

                                              print second_list


                                              Gives:



                                              ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']





                                              share|improve this answer

























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                if you want a one-liner, combine list comprehension with multiple if/else statements:



                                                first_list = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                                                second_list = [email + 'xtern@dom' if email.endswith(".e") else
                                                email if email.endswith("@dom") else "{}@dom".format(email)
                                                for email in first_list]

                                                print second_list


                                                Gives:



                                                ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']





                                                share|improve this answer














                                                if you want a one-liner, combine list comprehension with multiple if/else statements:



                                                first_list = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

                                                second_list = [email + 'xtern@dom' if email.endswith(".e") else
                                                email if email.endswith("@dom") else "{}@dom".format(email)
                                                for email in first_list]

                                                print second_list


                                                Gives:



                                                ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited yesterday

























                                                answered yesterday









                                                sudonym

                                                1,231824




                                                1,231824






















                                                    up vote
                                                    0
                                                    down vote













                                                    I came with a different solution in the end: Declaring a help function.



                                                    def mailfy(mail):
                                                    if mail.endswith(".c"):
                                                    return mail[:-2] + "@dom"
                                                    ...

                                                    mails = [mailfy(x) for x in mails]





                                                    share|improve this answer

























                                                      up vote
                                                      0
                                                      down vote













                                                      I came with a different solution in the end: Declaring a help function.



                                                      def mailfy(mail):
                                                      if mail.endswith(".c"):
                                                      return mail[:-2] + "@dom"
                                                      ...

                                                      mails = [mailfy(x) for x in mails]





                                                      share|improve this answer























                                                        up vote
                                                        0
                                                        down vote










                                                        up vote
                                                        0
                                                        down vote









                                                        I came with a different solution in the end: Declaring a help function.



                                                        def mailfy(mail):
                                                        if mail.endswith(".c"):
                                                        return mail[:-2] + "@dom"
                                                        ...

                                                        mails = [mailfy(x) for x in mails]





                                                        share|improve this answer












                                                        I came with a different solution in the end: Declaring a help function.



                                                        def mailfy(mail):
                                                        if mail.endswith(".c"):
                                                        return mail[:-2] + "@dom"
                                                        ...

                                                        mails = [mailfy(x) for x in mails]






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered yesterday









                                                        Hamperfait

                                                        125110




                                                        125110






























                                                             

                                                            draft saved


                                                            draft discarded



















































                                                             


                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function () {
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203425%2fpython-replace-in-list%23new-answer', 'question_page');
                                                            }
                                                            );

                                                            Post as a guest




















































































                                                            Popular posts from this blog

                                                            Landwehr

                                                            Reims

                                                            Schenkenzell