Comparing two lists using the greater than or less than operator











up vote
20
down vote

favorite
4












I noticed a piece of code recently directly comparing two lists of integers like so:



a = [10,3,5, ...]
b = [5,4,3, ...,]
if a > b:
...


which seemed a bit peculiar, but I imagined it would return True if all of list_a's elements are larger then list_b's and False if each element is equal or list_b's elements are larger then list_a's. So I tested it:



>>> a=[3,3,3,3]
>>> b=[4,4,4,4]
>>> a>b
False
>>> b>a
True


Ok that works. As does:



>>> b = [1,1,1,1]
>>> a = [1,1,1,1]
>>> a>b
False
>>> b>a
False


but when it gets more fuzzy:



>>> a=[1,1,3,1]
>>> b=[1,3,1,1]
>>> a>b
False
>>> b>a
True


or:



>>> a=[1,3,1,1]
>>> b=[1,1,3,3]
>>> a>b
True
>>> b>a
False


the results are a bit stranger. What is python actually doing? It seems that it's returning the result in favour of the first list in which the left most element is greater then the corresponding?










share|improve this question


























    up vote
    20
    down vote

    favorite
    4












    I noticed a piece of code recently directly comparing two lists of integers like so:



    a = [10,3,5, ...]
    b = [5,4,3, ...,]
    if a > b:
    ...


    which seemed a bit peculiar, but I imagined it would return True if all of list_a's elements are larger then list_b's and False if each element is equal or list_b's elements are larger then list_a's. So I tested it:



    >>> a=[3,3,3,3]
    >>> b=[4,4,4,4]
    >>> a>b
    False
    >>> b>a
    True


    Ok that works. As does:



    >>> b = [1,1,1,1]
    >>> a = [1,1,1,1]
    >>> a>b
    False
    >>> b>a
    False


    but when it gets more fuzzy:



    >>> a=[1,1,3,1]
    >>> b=[1,3,1,1]
    >>> a>b
    False
    >>> b>a
    True


    or:



    >>> a=[1,3,1,1]
    >>> b=[1,1,3,3]
    >>> a>b
    True
    >>> b>a
    False


    the results are a bit stranger. What is python actually doing? It seems that it's returning the result in favour of the first list in which the left most element is greater then the corresponding?










    share|improve this question
























      up vote
      20
      down vote

      favorite
      4









      up vote
      20
      down vote

      favorite
      4






      4





      I noticed a piece of code recently directly comparing two lists of integers like so:



      a = [10,3,5, ...]
      b = [5,4,3, ...,]
      if a > b:
      ...


      which seemed a bit peculiar, but I imagined it would return True if all of list_a's elements are larger then list_b's and False if each element is equal or list_b's elements are larger then list_a's. So I tested it:



      >>> a=[3,3,3,3]
      >>> b=[4,4,4,4]
      >>> a>b
      False
      >>> b>a
      True


      Ok that works. As does:



      >>> b = [1,1,1,1]
      >>> a = [1,1,1,1]
      >>> a>b
      False
      >>> b>a
      False


      but when it gets more fuzzy:



      >>> a=[1,1,3,1]
      >>> b=[1,3,1,1]
      >>> a>b
      False
      >>> b>a
      True


      or:



      >>> a=[1,3,1,1]
      >>> b=[1,1,3,3]
      >>> a>b
      True
      >>> b>a
      False


      the results are a bit stranger. What is python actually doing? It seems that it's returning the result in favour of the first list in which the left most element is greater then the corresponding?










      share|improve this question













      I noticed a piece of code recently directly comparing two lists of integers like so:



      a = [10,3,5, ...]
      b = [5,4,3, ...,]
      if a > b:
      ...


      which seemed a bit peculiar, but I imagined it would return True if all of list_a's elements are larger then list_b's and False if each element is equal or list_b's elements are larger then list_a's. So I tested it:



      >>> a=[3,3,3,3]
      >>> b=[4,4,4,4]
      >>> a>b
      False
      >>> b>a
      True


      Ok that works. As does:



      >>> b = [1,1,1,1]
      >>> a = [1,1,1,1]
      >>> a>b
      False
      >>> b>a
      False


      but when it gets more fuzzy:



      >>> a=[1,1,3,1]
      >>> b=[1,3,1,1]
      >>> a>b
      False
      >>> b>a
      True


      or:



      >>> a=[1,3,1,1]
      >>> b=[1,1,3,3]
      >>> a>b
      True
      >>> b>a
      False


      the results are a bit stranger. What is python actually doing? It seems that it's returning the result in favour of the first list in which the left most element is greater then the corresponding?







      python list






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 24 '12 at 15:51









      Timmy O'Mahony

      40k8115148




      40k8115148
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          33
          down vote



          accepted










          From Comparing Sequences and Other Types in the Python tutorial:




          The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.




          See also the Wikipedia article about lexicographical order.






          share|improve this answer






























            up vote
            3
            down vote













            Since I didn't find the explanation of list/tuple comparison using "lexicographical ordering" particularly illuminating at first, here's an attempt to explain it "in my own words". First, here are some example lists that are referred to in the explanation below:



            a = [1, 2, 3]
            b = [1, 2, 10]
            c = [1, 2, 3, 100]
            d = [1, 2, 3]
            e = [1, 2, 3, 4, 'a']
            f = ['a', 'b', 'c']


            The pair of items at each index are compared in turn. So, comparing a to b will result in 1 being compared to 1, 2 being compared to 2, and 3 being compared to 10.



            The comparison of pairs will stop when either an unequal pair of items is found or--if the lists are different lengths--the end of the shorter list is reached.



            For example, when comparing a and b, comparisons will stop when 3 and 10 are compared. When comparing b and c, comparisons will stop when 10 and 3 are compared.



            As soon as an unequal pair is found, the overall result is the result of comparing the unequal items. This applies whether the lists are the same length or not--for example, list b is greater than list c because the 100 in c never comes into play.



            For example, when comparing a to b, the overall result will be the result of comparing 3 to 10. a < b -> True because 3 is less than 10. a > b -> False because 3 is not greater than 10. a == b -> False because 3 does not equal 10.



            If one of the lists is shorter and its N items are equal to the first N items of the longer list, as with a and c, the shorter list will be considered less than the longer list (so a is less than c).



            Two lists will compare as equal only if they're the same length and all pairs of items compare as equal.



            Note about types: if the items in a pair aren't comparable, the comparison will fail with a TypeError as usual. For example, comparing list a to f will fail when 1 is compared to 'a'. But also note that lists d and e can be compared since the 'a' in e is never compared to anything in d.






            share|improve this answer






























              up vote
              0
              down vote













              You can use sum for finding the sum of all integers in the list and then you can compare them.






              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%2f13052857%2fcomparing-two-lists-using-the-greater-than-or-less-than-operator%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








                up vote
                33
                down vote



                accepted










                From Comparing Sequences and Other Types in the Python tutorial:




                The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.




                See also the Wikipedia article about lexicographical order.






                share|improve this answer



























                  up vote
                  33
                  down vote



                  accepted










                  From Comparing Sequences and Other Types in the Python tutorial:




                  The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.




                  See also the Wikipedia article about lexicographical order.






                  share|improve this answer

























                    up vote
                    33
                    down vote



                    accepted







                    up vote
                    33
                    down vote



                    accepted






                    From Comparing Sequences and Other Types in the Python tutorial:




                    The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.




                    See also the Wikipedia article about lexicographical order.






                    share|improve this answer














                    From Comparing Sequences and Other Types in the Python tutorial:




                    The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.




                    See also the Wikipedia article about lexicographical order.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 8 '17 at 15:47









                    aggieNick02

                    97711019




                    97711019










                    answered Oct 24 '12 at 15:54









                    gefei

                    13.1k33459




                    13.1k33459
























                        up vote
                        3
                        down vote













                        Since I didn't find the explanation of list/tuple comparison using "lexicographical ordering" particularly illuminating at first, here's an attempt to explain it "in my own words". First, here are some example lists that are referred to in the explanation below:



                        a = [1, 2, 3]
                        b = [1, 2, 10]
                        c = [1, 2, 3, 100]
                        d = [1, 2, 3]
                        e = [1, 2, 3, 4, 'a']
                        f = ['a', 'b', 'c']


                        The pair of items at each index are compared in turn. So, comparing a to b will result in 1 being compared to 1, 2 being compared to 2, and 3 being compared to 10.



                        The comparison of pairs will stop when either an unequal pair of items is found or--if the lists are different lengths--the end of the shorter list is reached.



                        For example, when comparing a and b, comparisons will stop when 3 and 10 are compared. When comparing b and c, comparisons will stop when 10 and 3 are compared.



                        As soon as an unequal pair is found, the overall result is the result of comparing the unequal items. This applies whether the lists are the same length or not--for example, list b is greater than list c because the 100 in c never comes into play.



                        For example, when comparing a to b, the overall result will be the result of comparing 3 to 10. a < b -> True because 3 is less than 10. a > b -> False because 3 is not greater than 10. a == b -> False because 3 does not equal 10.



                        If one of the lists is shorter and its N items are equal to the first N items of the longer list, as with a and c, the shorter list will be considered less than the longer list (so a is less than c).



                        Two lists will compare as equal only if they're the same length and all pairs of items compare as equal.



                        Note about types: if the items in a pair aren't comparable, the comparison will fail with a TypeError as usual. For example, comparing list a to f will fail when 1 is compared to 'a'. But also note that lists d and e can be compared since the 'a' in e is never compared to anything in d.






                        share|improve this answer



























                          up vote
                          3
                          down vote













                          Since I didn't find the explanation of list/tuple comparison using "lexicographical ordering" particularly illuminating at first, here's an attempt to explain it "in my own words". First, here are some example lists that are referred to in the explanation below:



                          a = [1, 2, 3]
                          b = [1, 2, 10]
                          c = [1, 2, 3, 100]
                          d = [1, 2, 3]
                          e = [1, 2, 3, 4, 'a']
                          f = ['a', 'b', 'c']


                          The pair of items at each index are compared in turn. So, comparing a to b will result in 1 being compared to 1, 2 being compared to 2, and 3 being compared to 10.



                          The comparison of pairs will stop when either an unequal pair of items is found or--if the lists are different lengths--the end of the shorter list is reached.



                          For example, when comparing a and b, comparisons will stop when 3 and 10 are compared. When comparing b and c, comparisons will stop when 10 and 3 are compared.



                          As soon as an unequal pair is found, the overall result is the result of comparing the unequal items. This applies whether the lists are the same length or not--for example, list b is greater than list c because the 100 in c never comes into play.



                          For example, when comparing a to b, the overall result will be the result of comparing 3 to 10. a < b -> True because 3 is less than 10. a > b -> False because 3 is not greater than 10. a == b -> False because 3 does not equal 10.



                          If one of the lists is shorter and its N items are equal to the first N items of the longer list, as with a and c, the shorter list will be considered less than the longer list (so a is less than c).



                          Two lists will compare as equal only if they're the same length and all pairs of items compare as equal.



                          Note about types: if the items in a pair aren't comparable, the comparison will fail with a TypeError as usual. For example, comparing list a to f will fail when 1 is compared to 'a'. But also note that lists d and e can be compared since the 'a' in e is never compared to anything in d.






                          share|improve this answer

























                            up vote
                            3
                            down vote










                            up vote
                            3
                            down vote









                            Since I didn't find the explanation of list/tuple comparison using "lexicographical ordering" particularly illuminating at first, here's an attempt to explain it "in my own words". First, here are some example lists that are referred to in the explanation below:



                            a = [1, 2, 3]
                            b = [1, 2, 10]
                            c = [1, 2, 3, 100]
                            d = [1, 2, 3]
                            e = [1, 2, 3, 4, 'a']
                            f = ['a', 'b', 'c']


                            The pair of items at each index are compared in turn. So, comparing a to b will result in 1 being compared to 1, 2 being compared to 2, and 3 being compared to 10.



                            The comparison of pairs will stop when either an unequal pair of items is found or--if the lists are different lengths--the end of the shorter list is reached.



                            For example, when comparing a and b, comparisons will stop when 3 and 10 are compared. When comparing b and c, comparisons will stop when 10 and 3 are compared.



                            As soon as an unequal pair is found, the overall result is the result of comparing the unequal items. This applies whether the lists are the same length or not--for example, list b is greater than list c because the 100 in c never comes into play.



                            For example, when comparing a to b, the overall result will be the result of comparing 3 to 10. a < b -> True because 3 is less than 10. a > b -> False because 3 is not greater than 10. a == b -> False because 3 does not equal 10.



                            If one of the lists is shorter and its N items are equal to the first N items of the longer list, as with a and c, the shorter list will be considered less than the longer list (so a is less than c).



                            Two lists will compare as equal only if they're the same length and all pairs of items compare as equal.



                            Note about types: if the items in a pair aren't comparable, the comparison will fail with a TypeError as usual. For example, comparing list a to f will fail when 1 is compared to 'a'. But also note that lists d and e can be compared since the 'a' in e is never compared to anything in d.






                            share|improve this answer














                            Since I didn't find the explanation of list/tuple comparison using "lexicographical ordering" particularly illuminating at first, here's an attempt to explain it "in my own words". First, here are some example lists that are referred to in the explanation below:



                            a = [1, 2, 3]
                            b = [1, 2, 10]
                            c = [1, 2, 3, 100]
                            d = [1, 2, 3]
                            e = [1, 2, 3, 4, 'a']
                            f = ['a', 'b', 'c']


                            The pair of items at each index are compared in turn. So, comparing a to b will result in 1 being compared to 1, 2 being compared to 2, and 3 being compared to 10.



                            The comparison of pairs will stop when either an unequal pair of items is found or--if the lists are different lengths--the end of the shorter list is reached.



                            For example, when comparing a and b, comparisons will stop when 3 and 10 are compared. When comparing b and c, comparisons will stop when 10 and 3 are compared.



                            As soon as an unequal pair is found, the overall result is the result of comparing the unequal items. This applies whether the lists are the same length or not--for example, list b is greater than list c because the 100 in c never comes into play.



                            For example, when comparing a to b, the overall result will be the result of comparing 3 to 10. a < b -> True because 3 is less than 10. a > b -> False because 3 is not greater than 10. a == b -> False because 3 does not equal 10.



                            If one of the lists is shorter and its N items are equal to the first N items of the longer list, as with a and c, the shorter list will be considered less than the longer list (so a is less than c).



                            Two lists will compare as equal only if they're the same length and all pairs of items compare as equal.



                            Note about types: if the items in a pair aren't comparable, the comparison will fail with a TypeError as usual. For example, comparing list a to f will fail when 1 is compared to 'a'. But also note that lists d and e can be compared since the 'a' in e is never compared to anything in d.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 17 '17 at 3:34

























                            answered Nov 17 '17 at 3:19







                            user8651755





























                                up vote
                                0
                                down vote













                                You can use sum for finding the sum of all integers in the list and then you can compare them.






                                share|improve this answer



























                                  up vote
                                  0
                                  down vote













                                  You can use sum for finding the sum of all integers in the list and then you can compare them.






                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    You can use sum for finding the sum of all integers in the list and then you can compare them.






                                    share|improve this answer














                                    You can use sum for finding the sum of all integers in the list and then you can compare them.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 11 at 18:04









                                    grrigore

                                    698820




                                    698820










                                    answered Nov 11 at 16:31









                                    TonyPlayZ

                                    1




                                    1






























                                        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%2f13052857%2fcomparing-two-lists-using-the-greater-than-or-less-than-operator%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

                                        Full-time equivalent

                                        Bicuculline

                                        さくらももこ