Remove first duplicate element of a list





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I need to remove the duplicate occurrence of the 1st element in the list which is duplicate (present more than once) while preserving the order of the input list. For eg: for the input of
in = [2, 3, 4, 5, 3, 6, 4, 1]
output should be
out = [2, 3, 4, 5, 6, 4, 1]



I have tried below and is giving correct result , I just wanted to check with the community if there is a better or more pythonic solution to this



input = [2, 3, 4, 5, 3, 6, 4, 1]
first_dupe = None
for elem in input:
if input.count(elem) > 1:
first_dupe = elem
break

flg = True
new_list =
for x in input:
if x != first_dupe or flg is True:
new_list.append(x)
if x == first_dupe:
flg = False

print(new_list)









share|improve this question





























    -1















    I need to remove the duplicate occurrence of the 1st element in the list which is duplicate (present more than once) while preserving the order of the input list. For eg: for the input of
    in = [2, 3, 4, 5, 3, 6, 4, 1]
    output should be
    out = [2, 3, 4, 5, 6, 4, 1]



    I have tried below and is giving correct result , I just wanted to check with the community if there is a better or more pythonic solution to this



    input = [2, 3, 4, 5, 3, 6, 4, 1]
    first_dupe = None
    for elem in input:
    if input.count(elem) > 1:
    first_dupe = elem
    break

    flg = True
    new_list =
    for x in input:
    if x != first_dupe or flg is True:
    new_list.append(x)
    if x == first_dupe:
    flg = False

    print(new_list)









    share|improve this question

























      -1












      -1








      -1








      I need to remove the duplicate occurrence of the 1st element in the list which is duplicate (present more than once) while preserving the order of the input list. For eg: for the input of
      in = [2, 3, 4, 5, 3, 6, 4, 1]
      output should be
      out = [2, 3, 4, 5, 6, 4, 1]



      I have tried below and is giving correct result , I just wanted to check with the community if there is a better or more pythonic solution to this



      input = [2, 3, 4, 5, 3, 6, 4, 1]
      first_dupe = None
      for elem in input:
      if input.count(elem) > 1:
      first_dupe = elem
      break

      flg = True
      new_list =
      for x in input:
      if x != first_dupe or flg is True:
      new_list.append(x)
      if x == first_dupe:
      flg = False

      print(new_list)









      share|improve this question














      I need to remove the duplicate occurrence of the 1st element in the list which is duplicate (present more than once) while preserving the order of the input list. For eg: for the input of
      in = [2, 3, 4, 5, 3, 6, 4, 1]
      output should be
      out = [2, 3, 4, 5, 6, 4, 1]



      I have tried below and is giving correct result , I just wanted to check with the community if there is a better or more pythonic solution to this



      input = [2, 3, 4, 5, 3, 6, 4, 1]
      first_dupe = None
      for elem in input:
      if input.count(elem) > 1:
      first_dupe = elem
      break

      flg = True
      new_list =
      for x in input:
      if x != first_dupe or flg is True:
      new_list.append(x)
      if x == first_dupe:
      flg = False

      print(new_list)






      python list






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 2:45









      ShanilShanil

      5717




      5717
























          3 Answers
          3






          active

          oldest

          votes


















          2














          If you just want to remove the first duplicate, you can create set, append elements to a new list if they are in the set while removing each element from the set as well. When an element is not seen, append the rest of the list.



          This is fairly efficient if the duplicate is seen early, but has O(1) if the element is seen late.



          x = [2, 3, 4, 5, 3, 6, 4, 1]
          s = set(x)
          out =

          for i,z in enumerate(x):
          if z in s:
          out.append(z)
          s.remove(z)
          else:
          break

          out += x[i+1:]

          out
          # returns:
          [2, 3, 4, 5, 6, 4, 1]





          share|improve this answer































            0














            You could keep track of what you have already used and check if the value is in there.



            lst = [2, 3, 4, 5, 3, 6, 4, 1]

            used = set()
            for i, x in enumerate(lst):
            if x in used:
            lst.pop(i)
            break
            used.add(x)
            print(lst)


            Also, don't give variables, or anything else, the same name as a keyword in python. input is already a built-in function.






            share|improve this answer


























            • It's better for used to be set.

              – dyukha
              Jan 3 at 2:55











            • This is better than the original solution, but: 1. Iterating using for i in range(len()) is not pythonic. 2. used could probably be a set.

              – Tomothy32
              Jan 3 at 2:55











            • Thanks, I guess the best alternative would be enumerate?

              – All Knower
              Jan 3 at 3:22



















            0














            This is my version of Chiheb Nexus' answer.



            def find_dup(lst):
            seen = set()
            it = iter(lst)
            for item in it:
            if item not in seen:
            seen.add(item)
            yield item
            else:
            yield from it

            lst = [2, 3, 4, 5, 3, 6, 4, 1]
            list(find_dup(lst))




            [2, 3, 4, 5, 6, 4, 1]





            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%2f54015685%2fremove-first-duplicate-element-of-a-list%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              If you just want to remove the first duplicate, you can create set, append elements to a new list if they are in the set while removing each element from the set as well. When an element is not seen, append the rest of the list.



              This is fairly efficient if the duplicate is seen early, but has O(1) if the element is seen late.



              x = [2, 3, 4, 5, 3, 6, 4, 1]
              s = set(x)
              out =

              for i,z in enumerate(x):
              if z in s:
              out.append(z)
              s.remove(z)
              else:
              break

              out += x[i+1:]

              out
              # returns:
              [2, 3, 4, 5, 6, 4, 1]





              share|improve this answer




























                2














                If you just want to remove the first duplicate, you can create set, append elements to a new list if they are in the set while removing each element from the set as well. When an element is not seen, append the rest of the list.



                This is fairly efficient if the duplicate is seen early, but has O(1) if the element is seen late.



                x = [2, 3, 4, 5, 3, 6, 4, 1]
                s = set(x)
                out =

                for i,z in enumerate(x):
                if z in s:
                out.append(z)
                s.remove(z)
                else:
                break

                out += x[i+1:]

                out
                # returns:
                [2, 3, 4, 5, 6, 4, 1]





                share|improve this answer


























                  2












                  2








                  2







                  If you just want to remove the first duplicate, you can create set, append elements to a new list if they are in the set while removing each element from the set as well. When an element is not seen, append the rest of the list.



                  This is fairly efficient if the duplicate is seen early, but has O(1) if the element is seen late.



                  x = [2, 3, 4, 5, 3, 6, 4, 1]
                  s = set(x)
                  out =

                  for i,z in enumerate(x):
                  if z in s:
                  out.append(z)
                  s.remove(z)
                  else:
                  break

                  out += x[i+1:]

                  out
                  # returns:
                  [2, 3, 4, 5, 6, 4, 1]





                  share|improve this answer













                  If you just want to remove the first duplicate, you can create set, append elements to a new list if they are in the set while removing each element from the set as well. When an element is not seen, append the rest of the list.



                  This is fairly efficient if the duplicate is seen early, but has O(1) if the element is seen late.



                  x = [2, 3, 4, 5, 3, 6, 4, 1]
                  s = set(x)
                  out =

                  for i,z in enumerate(x):
                  if z in s:
                  out.append(z)
                  s.remove(z)
                  else:
                  break

                  out += x[i+1:]

                  out
                  # returns:
                  [2, 3, 4, 5, 6, 4, 1]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 2:56









                  JamesJames

                  13.8k11633




                  13.8k11633

























                      0














                      You could keep track of what you have already used and check if the value is in there.



                      lst = [2, 3, 4, 5, 3, 6, 4, 1]

                      used = set()
                      for i, x in enumerate(lst):
                      if x in used:
                      lst.pop(i)
                      break
                      used.add(x)
                      print(lst)


                      Also, don't give variables, or anything else, the same name as a keyword in python. input is already a built-in function.






                      share|improve this answer


























                      • It's better for used to be set.

                        – dyukha
                        Jan 3 at 2:55











                      • This is better than the original solution, but: 1. Iterating using for i in range(len()) is not pythonic. 2. used could probably be a set.

                        – Tomothy32
                        Jan 3 at 2:55











                      • Thanks, I guess the best alternative would be enumerate?

                        – All Knower
                        Jan 3 at 3:22
















                      0














                      You could keep track of what you have already used and check if the value is in there.



                      lst = [2, 3, 4, 5, 3, 6, 4, 1]

                      used = set()
                      for i, x in enumerate(lst):
                      if x in used:
                      lst.pop(i)
                      break
                      used.add(x)
                      print(lst)


                      Also, don't give variables, or anything else, the same name as a keyword in python. input is already a built-in function.






                      share|improve this answer


























                      • It's better for used to be set.

                        – dyukha
                        Jan 3 at 2:55











                      • This is better than the original solution, but: 1. Iterating using for i in range(len()) is not pythonic. 2. used could probably be a set.

                        – Tomothy32
                        Jan 3 at 2:55











                      • Thanks, I guess the best alternative would be enumerate?

                        – All Knower
                        Jan 3 at 3:22














                      0












                      0








                      0







                      You could keep track of what you have already used and check if the value is in there.



                      lst = [2, 3, 4, 5, 3, 6, 4, 1]

                      used = set()
                      for i, x in enumerate(lst):
                      if x in used:
                      lst.pop(i)
                      break
                      used.add(x)
                      print(lst)


                      Also, don't give variables, or anything else, the same name as a keyword in python. input is already a built-in function.






                      share|improve this answer















                      You could keep track of what you have already used and check if the value is in there.



                      lst = [2, 3, 4, 5, 3, 6, 4, 1]

                      used = set()
                      for i, x in enumerate(lst):
                      if x in used:
                      lst.pop(i)
                      break
                      used.add(x)
                      print(lst)


                      Also, don't give variables, or anything else, the same name as a keyword in python. input is already a built-in function.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jan 3 at 3:42

























                      answered Jan 3 at 2:53









                      All KnowerAll Knower

                      413214




                      413214













                      • It's better for used to be set.

                        – dyukha
                        Jan 3 at 2:55











                      • This is better than the original solution, but: 1. Iterating using for i in range(len()) is not pythonic. 2. used could probably be a set.

                        – Tomothy32
                        Jan 3 at 2:55











                      • Thanks, I guess the best alternative would be enumerate?

                        – All Knower
                        Jan 3 at 3:22



















                      • It's better for used to be set.

                        – dyukha
                        Jan 3 at 2:55











                      • This is better than the original solution, but: 1. Iterating using for i in range(len()) is not pythonic. 2. used could probably be a set.

                        – Tomothy32
                        Jan 3 at 2:55











                      • Thanks, I guess the best alternative would be enumerate?

                        – All Knower
                        Jan 3 at 3:22

















                      It's better for used to be set.

                      – dyukha
                      Jan 3 at 2:55





                      It's better for used to be set.

                      – dyukha
                      Jan 3 at 2:55













                      This is better than the original solution, but: 1. Iterating using for i in range(len()) is not pythonic. 2. used could probably be a set.

                      – Tomothy32
                      Jan 3 at 2:55





                      This is better than the original solution, but: 1. Iterating using for i in range(len()) is not pythonic. 2. used could probably be a set.

                      – Tomothy32
                      Jan 3 at 2:55













                      Thanks, I guess the best alternative would be enumerate?

                      – All Knower
                      Jan 3 at 3:22





                      Thanks, I guess the best alternative would be enumerate?

                      – All Knower
                      Jan 3 at 3:22











                      0














                      This is my version of Chiheb Nexus' answer.



                      def find_dup(lst):
                      seen = set()
                      it = iter(lst)
                      for item in it:
                      if item not in seen:
                      seen.add(item)
                      yield item
                      else:
                      yield from it

                      lst = [2, 3, 4, 5, 3, 6, 4, 1]
                      list(find_dup(lst))




                      [2, 3, 4, 5, 6, 4, 1]





                      share|improve this answer




























                        0














                        This is my version of Chiheb Nexus' answer.



                        def find_dup(lst):
                        seen = set()
                        it = iter(lst)
                        for item in it:
                        if item not in seen:
                        seen.add(item)
                        yield item
                        else:
                        yield from it

                        lst = [2, 3, 4, 5, 3, 6, 4, 1]
                        list(find_dup(lst))




                        [2, 3, 4, 5, 6, 4, 1]





                        share|improve this answer


























                          0












                          0








                          0







                          This is my version of Chiheb Nexus' answer.



                          def find_dup(lst):
                          seen = set()
                          it = iter(lst)
                          for item in it:
                          if item not in seen:
                          seen.add(item)
                          yield item
                          else:
                          yield from it

                          lst = [2, 3, 4, 5, 3, 6, 4, 1]
                          list(find_dup(lst))




                          [2, 3, 4, 5, 6, 4, 1]





                          share|improve this answer













                          This is my version of Chiheb Nexus' answer.



                          def find_dup(lst):
                          seen = set()
                          it = iter(lst)
                          for item in it:
                          if item not in seen:
                          seen.add(item)
                          yield item
                          else:
                          yield from it

                          lst = [2, 3, 4, 5, 3, 6, 4, 1]
                          list(find_dup(lst))




                          [2, 3, 4, 5, 6, 4, 1]






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 3 at 5:03









                          John La RooyJohn La Rooy

                          215k41279431




                          215k41279431






























                              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%2f54015685%2fremove-first-duplicate-element-of-a-list%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

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith