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 yesterday









      mehrdad-pedramfar

      3,32511232




      3,32511232










      asked yesterday









      Jamie O connor

      306




      306
























          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.
            – Jamie O connor
            yesterday












          • I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
            – quamrana
            yesterday






          • 1




            sorry I forgot .items(). it is fixed now.@quamrana
            – mehrdad-pedramfar
            yesterday













          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
































          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.
            – Jamie O connor
            yesterday












          • I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
            – quamrana
            yesterday






          • 1




            sorry I forgot .items(). it is fixed now.@quamrana
            – mehrdad-pedramfar
            yesterday

















          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.
            – Jamie O connor
            yesterday












          • I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
            – quamrana
            yesterday






          • 1




            sorry I forgot .items(). it is fixed now.@quamrana
            – mehrdad-pedramfar
            yesterday















          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 yesterday

























          answered yesterday









          mehrdad-pedramfar

          3,32511232




          3,32511232








          • 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.
            – Jamie O connor
            yesterday












          • I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
            – quamrana
            yesterday






          • 1




            sorry I forgot .items(). it is fixed now.@quamrana
            – mehrdad-pedramfar
            yesterday
















          • 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.
            – Jamie O connor
            yesterday












          • I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
            – quamrana
            yesterday






          • 1




            sorry I forgot .items(). it is fixed now.@quamrana
            – mehrdad-pedramfar
            yesterday










          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.
          – Jamie O connor
          yesterday






          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.
          – Jamie O connor
          yesterday














          I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
          – quamrana
          yesterday




          I get TypeError: 'int' object is not iterable for the line: for k,v in c:.
          – quamrana
          yesterday




          1




          1




          sorry I forgot .items(). it is fixed now.@quamrana
          – mehrdad-pedramfar
          yesterday






          sorry I forgot .items(). it is fixed now.@quamrana
          – mehrdad-pedramfar
          yesterday




















           

          draft saved


          draft discarded



















































           


          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




















































































          Popular posts from this blog

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation