How do you check if a list's item is equal to another item in the same list









up vote
1
down vote

favorite












I understand the basics of this problem however I need help on how I can do this the most efficient way possible (taking the least amount of time for the programmer however not substituting stability of the code or efficiency).



Let's say we have a string:



grades=str(input("Enter a string"))


in my code, I would join a space between all characters in the string above and then split the characters into separate items in the same list:



grades=" ".join(grades)
grades.split(" ")


I then want to use loops of some sort to search the list for repeating items. However, I want to learn how I can do this the most efficient way possible:



x=len(grades)
for i in range(0, x):
if grades[i] == # here is were I'm having trouble


I want to know how I can search whether 1 item in the list is equal to any item in the whole list itself. Kind regards.










share|improve this question



























    up vote
    1
    down vote

    favorite












    I understand the basics of this problem however I need help on how I can do this the most efficient way possible (taking the least amount of time for the programmer however not substituting stability of the code or efficiency).



    Let's say we have a string:



    grades=str(input("Enter a string"))


    in my code, I would join a space between all characters in the string above and then split the characters into separate items in the same list:



    grades=" ".join(grades)
    grades.split(" ")


    I then want to use loops of some sort to search the list for repeating items. However, I want to learn how I can do this the most efficient way possible:



    x=len(grades)
    for i in range(0, x):
    if grades[i] == # here is were I'm having trouble


    I want to know how I can search whether 1 item in the list is equal to any item in the whole list itself. Kind regards.










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I understand the basics of this problem however I need help on how I can do this the most efficient way possible (taking the least amount of time for the programmer however not substituting stability of the code or efficiency).



      Let's say we have a string:



      grades=str(input("Enter a string"))


      in my code, I would join a space between all characters in the string above and then split the characters into separate items in the same list:



      grades=" ".join(grades)
      grades.split(" ")


      I then want to use loops of some sort to search the list for repeating items. However, I want to learn how I can do this the most efficient way possible:



      x=len(grades)
      for i in range(0, x):
      if grades[i] == # here is were I'm having trouble


      I want to know how I can search whether 1 item in the list is equal to any item in the whole list itself. Kind regards.










      share|improve this question















      I understand the basics of this problem however I need help on how I can do this the most efficient way possible (taking the least amount of time for the programmer however not substituting stability of the code or efficiency).



      Let's say we have a string:



      grades=str(input("Enter a string"))


      in my code, I would join a space between all characters in the string above and then split the characters into separate items in the same list:



      grades=" ".join(grades)
      grades.split(" ")


      I then want to use loops of some sort to search the list for repeating items. However, I want to learn how I can do this the most efficient way possible:



      x=len(grades)
      for i in range(0, x):
      if grades[i] == # here is were I'm having trouble


      I want to know how I can search whether 1 item in the list is equal to any item in the whole list itself. Kind regards.







      python python-3.x list






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 10:56









      mehrdad-pedramfar

      4,02511234




      4,02511234










      asked Nov 10 at 10:47









      Coder Cody

      408




      408






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          I make an example:



          from collections import Counter

          a =[1,2,3,4,1,2]
          c = Counter(a)
          for k,v in c.items():
          if v>1:
          print(k,'repeated more than once')


          Here the c will be a Counter object like this Counter(1: 2, 2: 2, 3: 1, 4: 1). the keys are the array values and values are the count of them.
          So I write the for for your understanding. You can do anything with c, it acts like a dict.



           >> [k for k,v in c.items() if v>1]
          [1, 2]





          share|improve this answer


















          • 2




            Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
            – Coder Cody
            Nov 10 at 10:57











          • I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
            – quamrana
            Nov 10 at 11:01






          • 1




            sorry I forgot .items(). it is fixed now.@quamrana
            – mehrdad-pedramfar
            Nov 10 at 11:02











          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%2f53238167%2fhow-do-you-check-if-a-lists-item-is-equal-to-another-item-in-the-same-list%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          I make an example:



          from collections import Counter

          a =[1,2,3,4,1,2]
          c = Counter(a)
          for k,v in c.items():
          if v>1:
          print(k,'repeated more than once')


          Here the c will be a Counter object like this Counter(1: 2, 2: 2, 3: 1, 4: 1). the keys are the array values and values are the count of them.
          So I write the for for your understanding. You can do anything with c, it acts like a dict.



           >> [k for k,v in c.items() if v>1]
          [1, 2]





          share|improve this answer


















          • 2




            Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
            – Coder Cody
            Nov 10 at 10:57











          • I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
            – quamrana
            Nov 10 at 11:01






          • 1




            sorry I forgot .items(). it is fixed now.@quamrana
            – mehrdad-pedramfar
            Nov 10 at 11:02















          up vote
          4
          down vote



          accepted










          I make an example:



          from collections import Counter

          a =[1,2,3,4,1,2]
          c = Counter(a)
          for k,v in c.items():
          if v>1:
          print(k,'repeated more than once')


          Here the c will be a Counter object like this Counter(1: 2, 2: 2, 3: 1, 4: 1). the keys are the array values and values are the count of them.
          So I write the for for your understanding. You can do anything with c, it acts like a dict.



           >> [k for k,v in c.items() if v>1]
          [1, 2]





          share|improve this answer


















          • 2




            Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
            – Coder Cody
            Nov 10 at 10:57











          • I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
            – quamrana
            Nov 10 at 11:01






          • 1




            sorry I forgot .items(). it is fixed now.@quamrana
            – mehrdad-pedramfar
            Nov 10 at 11:02













          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          I make an example:



          from collections import Counter

          a =[1,2,3,4,1,2]
          c = Counter(a)
          for k,v in c.items():
          if v>1:
          print(k,'repeated more than once')


          Here the c will be a Counter object like this Counter(1: 2, 2: 2, 3: 1, 4: 1). the keys are the array values and values are the count of them.
          So I write the for for your understanding. You can do anything with c, it acts like a dict.



           >> [k for k,v in c.items() if v>1]
          [1, 2]





          share|improve this answer














          I make an example:



          from collections import Counter

          a =[1,2,3,4,1,2]
          c = Counter(a)
          for k,v in c.items():
          if v>1:
          print(k,'repeated more than once')


          Here the c will be a Counter object like this Counter(1: 2, 2: 2, 3: 1, 4: 1). the keys are the array values and values are the count of them.
          So I write the for for your understanding. You can do anything with c, it acts like a dict.



           >> [k for k,v in c.items() if v>1]
          [1, 2]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 11:01

























          answered Nov 10 at 10:51









          mehrdad-pedramfar

          4,02511234




          4,02511234







          • 2




            Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
            – Coder Cody
            Nov 10 at 10:57











          • I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
            – quamrana
            Nov 10 at 11:01






          • 1




            sorry I forgot .items(). it is fixed now.@quamrana
            – mehrdad-pedramfar
            Nov 10 at 11:02













          • 2




            Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
            – Coder Cody
            Nov 10 at 10:57











          • I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
            – quamrana
            Nov 10 at 11:01






          • 1




            sorry I forgot .items(). it is fixed now.@quamrana
            – mehrdad-pedramfar
            Nov 10 at 11:02








          2




          2




          Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
          – Coder Cody
          Nov 10 at 10:57





          Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
          – Coder Cody
          Nov 10 at 10:57













          I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
          – quamrana
          Nov 10 at 11:01




          I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
          – quamrana
          Nov 10 at 11:01




          1




          1




          sorry I forgot .items(). it is fixed now.@quamrana
          – mehrdad-pedramfar
          Nov 10 at 11:02





          sorry I forgot .items(). it is fixed now.@quamrana
          – mehrdad-pedramfar
          Nov 10 at 11:02


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53238167%2fhow-do-you-check-if-a-lists-item-is-equal-to-another-item-in-the-same-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

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20