Python list: check if all items are the same or not











up vote
0
down vote

favorite












I have a python list with strings and i want to check if all list items values are the same or not.
I tried to use with condition if/then but i need to check all combinations of list values and if list have many items then need to many hard code



if item1 != item1 and item1 != item2 and item1 !=item2 ....... :
check='wrong'
else:
check= 'correct'


List of string



listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']


UPDATE



EXAMPLE :



CORRECT_LIST = ['ep:1000' , 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']


IN correct list all items values are the same, then my list is correct



WRONG_LIST = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']


WRONG_LIST in the wrong list is not all items values string are the some



I want do do this check for list values.



Any idea how to check if all items values in list are the same or not ?










share|improve this question




















  • 2




    "i want to check if all list items values is the some or not.", what do you mean with this?
    – Willem Van Onsem
    yesterday






  • 1




    Can you explain with an example?
    – Austin
    yesterday










  • Austin and Willem Van Onsem i update my question
    – Mar
    yesterday















up vote
0
down vote

favorite












I have a python list with strings and i want to check if all list items values are the same or not.
I tried to use with condition if/then but i need to check all combinations of list values and if list have many items then need to many hard code



if item1 != item1 and item1 != item2 and item1 !=item2 ....... :
check='wrong'
else:
check= 'correct'


List of string



listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']


UPDATE



EXAMPLE :



CORRECT_LIST = ['ep:1000' , 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']


IN correct list all items values are the same, then my list is correct



WRONG_LIST = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']


WRONG_LIST in the wrong list is not all items values string are the some



I want do do this check for list values.



Any idea how to check if all items values in list are the same or not ?










share|improve this question




















  • 2




    "i want to check if all list items values is the some or not.", what do you mean with this?
    – Willem Van Onsem
    yesterday






  • 1




    Can you explain with an example?
    – Austin
    yesterday










  • Austin and Willem Van Onsem i update my question
    – Mar
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a python list with strings and i want to check if all list items values are the same or not.
I tried to use with condition if/then but i need to check all combinations of list values and if list have many items then need to many hard code



if item1 != item1 and item1 != item2 and item1 !=item2 ....... :
check='wrong'
else:
check= 'correct'


List of string



listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']


UPDATE



EXAMPLE :



CORRECT_LIST = ['ep:1000' , 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']


IN correct list all items values are the same, then my list is correct



WRONG_LIST = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']


WRONG_LIST in the wrong list is not all items values string are the some



I want do do this check for list values.



Any idea how to check if all items values in list are the same or not ?










share|improve this question















I have a python list with strings and i want to check if all list items values are the same or not.
I tried to use with condition if/then but i need to check all combinations of list values and if list have many items then need to many hard code



if item1 != item1 and item1 != item2 and item1 !=item2 ....... :
check='wrong'
else:
check= 'correct'


List of string



listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']


UPDATE



EXAMPLE :



CORRECT_LIST = ['ep:1000' , 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']


IN correct list all items values are the same, then my list is correct



WRONG_LIST = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']


WRONG_LIST in the wrong list is not all items values string are the some



I want do do this check for list values.



Any idea how to check if all items values in list are the same or not ?







python list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









iGian

2,2832620




2,2832620










asked yesterday









Mar

236110




236110








  • 2




    "i want to check if all list items values is the some or not.", what do you mean with this?
    – Willem Van Onsem
    yesterday






  • 1




    Can you explain with an example?
    – Austin
    yesterday










  • Austin and Willem Van Onsem i update my question
    – Mar
    yesterday














  • 2




    "i want to check if all list items values is the some or not.", what do you mean with this?
    – Willem Van Onsem
    yesterday






  • 1




    Can you explain with an example?
    – Austin
    yesterday










  • Austin and Willem Van Onsem i update my question
    – Mar
    yesterday








2




2




"i want to check if all list items values is the some or not.", what do you mean with this?
– Willem Van Onsem
yesterday




"i want to check if all list items values is the some or not.", what do you mean with this?
– Willem Van Onsem
yesterday




1




1




Can you explain with an example?
– Austin
yesterday




Can you explain with an example?
– Austin
yesterday












Austin and Willem Van Onsem i update my question
– Mar
yesterday




Austin and Willem Van Onsem i update my question
– Mar
yesterday












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










This compares every element of the list to the first one:



listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
check = all(x == listOfStrings[0] for x in listOfStrings)


And returns false for your test case.






share|improve this answer




























    up vote
    4
    down vote













    The code snippet you have provided looks a little weird. But if I understand correctly, you are trying to check for the number of unique values in a list.



    One way to do it is to convert it to a set and check its length.



    len(set(listOfStrings))


    Updated to include working code snippet from @iGian:



    check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'





    share|improve this answer










    New contributor




    boonwj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.














    • 1




      check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
      – iGian
      yesterday




















    up vote
    1
    down vote













    IN correct list all items values are the same, then my list is correct



    If you want to check if all items in a list are the same you can check if the lenght of the set of the list is equal to 1:



    set((listOfStrings)) == 1 


    Characteristic for a set is that every element is unique namely to that set.






    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%2f53238409%2fpython-list-check-if-all-items-are-the-same-or-not%23new-answer', 'question_page');
      }
      );

      Post as a guest
































      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      This compares every element of the list to the first one:



      listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
      check = all(x == listOfStrings[0] for x in listOfStrings)


      And returns false for your test case.






      share|improve this answer

























        up vote
        1
        down vote



        accepted










        This compares every element of the list to the first one:



        listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
        check = all(x == listOfStrings[0] for x in listOfStrings)


        And returns false for your test case.






        share|improve this answer























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          This compares every element of the list to the first one:



          listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
          check = all(x == listOfStrings[0] for x in listOfStrings)


          And returns false for your test case.






          share|improve this answer












          This compares every element of the list to the first one:



          listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
          check = all(x == listOfStrings[0] for x in listOfStrings)


          And returns false for your test case.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          b-fg

          73711022




          73711022
























              up vote
              4
              down vote













              The code snippet you have provided looks a little weird. But if I understand correctly, you are trying to check for the number of unique values in a list.



              One way to do it is to convert it to a set and check its length.



              len(set(listOfStrings))


              Updated to include working code snippet from @iGian:



              check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'





              share|improve this answer










              New contributor




              boonwj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.














              • 1




                check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
                – iGian
                yesterday

















              up vote
              4
              down vote













              The code snippet you have provided looks a little weird. But if I understand correctly, you are trying to check for the number of unique values in a list.



              One way to do it is to convert it to a set and check its length.



              len(set(listOfStrings))


              Updated to include working code snippet from @iGian:



              check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'





              share|improve this answer










              New contributor




              boonwj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.














              • 1




                check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
                – iGian
                yesterday















              up vote
              4
              down vote










              up vote
              4
              down vote









              The code snippet you have provided looks a little weird. But if I understand correctly, you are trying to check for the number of unique values in a list.



              One way to do it is to convert it to a set and check its length.



              len(set(listOfStrings))


              Updated to include working code snippet from @iGian:



              check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'





              share|improve this answer










              New contributor




              boonwj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              The code snippet you have provided looks a little weird. But if I understand correctly, you are trying to check for the number of unique values in a list.



              One way to do it is to convert it to a set and check its length.



              len(set(listOfStrings))


              Updated to include working code snippet from @iGian:



              check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'






              share|improve this answer










              New contributor




              boonwj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              share|improve this answer



              share|improve this answer








              edited yesterday





















              New contributor




              boonwj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              answered yesterday









              boonwj

              1014




              1014




              New contributor




              boonwj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





              New contributor





              boonwj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              boonwj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.








              • 1




                check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
                – iGian
                yesterday
















              • 1




                check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
                – iGian
                yesterday










              1




              1




              check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
              – iGian
              yesterday






              check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
              – iGian
              yesterday












              up vote
              1
              down vote













              IN correct list all items values are the same, then my list is correct



              If you want to check if all items in a list are the same you can check if the lenght of the set of the list is equal to 1:



              set((listOfStrings)) == 1 


              Characteristic for a set is that every element is unique namely to that set.






              share|improve this answer



























                up vote
                1
                down vote













                IN correct list all items values are the same, then my list is correct



                If you want to check if all items in a list are the same you can check if the lenght of the set of the list is equal to 1:



                set((listOfStrings)) == 1 


                Characteristic for a set is that every element is unique namely to that set.






                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  IN correct list all items values are the same, then my list is correct



                  If you want to check if all items in a list are the same you can check if the lenght of the set of the list is equal to 1:



                  set((listOfStrings)) == 1 


                  Characteristic for a set is that every element is unique namely to that set.






                  share|improve this answer














                  IN correct list all items values are the same, then my list is correct



                  If you want to check if all items in a list are the same you can check if the lenght of the set of the list is equal to 1:



                  set((listOfStrings)) == 1 


                  Characteristic for a set is that every element is unique namely to that set.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited yesterday

























                  answered yesterday









                  petruz

                  461311




                  461311






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238409%2fpython-list-check-if-all-items-are-the-same-or-not%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest




















































































                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python