{"id":42107,"date":"2023-02-15T09:58:11","date_gmt":"2023-02-15T09:58:11","guid":{"rendered":"https:\/\/www.askpython.com\/?p=42107"},"modified":"2023-02-16T19:56:16","modified_gmt":"2023-02-16T19:56:16","slug":"tuple-comparison","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/tuple\/tuple-comparison","title":{"rendered":"How does tuple comparison work in Python?"},"content":{"rendered":"\n<p>Python has four built-in data types that are used to store collections of data. These Python data types are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.askpython.com\/python\/list\" target=\"_blank\" rel=\"noreferrer noopener\">List<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python\/set\" target=\"_blank\" rel=\"noreferrer noopener\">Set<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python\/dictionary\" target=\"_blank\" rel=\"noreferrer noopener\">Dictionary<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python\/tuple\" target=\"_blank\" rel=\"noreferrer noopener\">Tuple<\/a><\/li>\n<\/ul>\n\n\n\n<p>A tuple in Python is a collection of data items or objects stored inside a single variable. The data items inside a tuple are separated by commas. Syntactically, tuples store data inside round brackets i.e. <strong>(&lt;insert elements separated by commas>)<\/strong>.<\/p>\n\n\n\n<p>Tuples in Python are <strong>immutable<\/strong>, meaning that once a tuple is created, it cannot be changed. It is <strong>unchangeable<\/strong>. Also, tuples are an <strong>ordered<\/strong> collection of data. <\/p>\n\n\n\n<p>This implies that tuples are 0-indexed and the data items inside a tuple can be directly accessed using their index. It also supports negative indexing, just like lists and strings in Python. Tuples in Python can hold elements of the same data type or of different types as well. That is, tuples can be both <strong>homogenous<\/strong> and <strong>heterogeneous<\/strong>.<\/p>\n\n\n\n<p><strong>Below are some examples of tuples in Python:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntuple1 = (45, 2, 67, 7)\n\ntuple2 = (&#039;cat&#039;, &#039;fish&#039;, &#039;dog&#039;)\n\ntuple3 = (52.5, 36.4, 0.2)\n\ntuple4 = (4, 8.6, &#039;hey&#039;) \n<\/pre><\/div>\n\n\n<p>In this tutorial, you will learn how tuples are compared in Python and also go through some examples regarding the same.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Tuple Comparison in Python<\/h2>\n\n\n\n<p>In Python, a tuple comparison is a method for comparing two or more tuples based on their elements and establishing their relative order.<\/p>\n\n\n\n<p><strong>Tuples are compared lexicographically<\/strong>. This implies that the corresponding elements of the tuples under consideration are compared to find the relative order. Hence, the comparison is performed elementwise i.e., the first element from the first tuple is compared with the first element from the second tuple and so on. <\/p>\n\n\n\n<p>Sometimes the first comparison is enough to conclude the result but if it is not then the tuples are compared further until all the elements are compared or we get a result. <\/p>\n\n\n\n<p>Tuples can be compared using <a href=\"https:\/\/www.askpython.com\/python\/python-comparison-operators\" target=\"_blank\" rel=\"noreferrer noopener\">comparison operators<\/a> like <strong>>, >=, &lt;, &lt;=, == and !=.<\/strong>  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Important points to note<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>While comparing two data collections in Python, they should be of the same type. Comparing a list with a tuple will result in an error.<\/li>\n\n\n\n<li>For two collections to be equal, they must be of the same data type, should have the same length and each pair of corresponding elements must be equal. <\/li>\n\n\n\n<li>Tuple comparison in Python is lexicographical. A tuple being &#8216;<em>greater than<\/em>&#8216; another means it appears <em>after<\/em> the other. Similarly, a tuple is &#8216;<em>smaller than<\/em>&#8216; another implies that is appears <em>before<\/em> the other. <\/li>\n\n\n\n<li>A tuple is &#8216;greater than&#8217; another if an element in it is greater than the correspoding element the other tuple. Similar rule can be deduced for the other comparison operators.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Performing comparison of tuples using Python<\/h2>\n\n\n\n<p>Have a look at the below code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = (5, 6, 10)\nb = (4, 12, 7)\n\nprint(&#039;Tuple a:&#039;, a, &#039;\\nTuple b:&#039;, b)\n\nprint(&#039;\\nTuple a &gt; Tuple b?:&#039;, a&gt;b)\n\nprint(&#039;\\nTuple a &lt; Tuple b?:&#039;, a&lt;b)\n\nprint(&#039;\\nTuple a == Tuple b?:&#039;, a==b)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nTuple a: (5, 6, 10) \nTuple b: (4, 12, 7)\n\nTuple a &gt; Tuple b?: True\n\nTuple a &amp;lt; Tuple b?: False\n\nTuple a == Tuple b?: False\n<\/pre><\/div>\n\n\n<p>This example contains two tuples <strong>a<\/strong> and <strong>b<\/strong>. These tuples are compared as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><span style=\"text-decoration: underline\">Greater than<\/span>: When comparing the two tuples, the first elements in both the tuples are compared first. Since 5&gt;4, it is established that <strong>tuple a is greater than tuples b<\/strong> and the result is <strong>True<\/strong>. No further comparisons are made as it is not needed.<\/li>\n\n\n\n<li><span style=\"text-decoration: underline\">Smaller than<\/span>: Comparing the first elements, 5 is not smaller than 4, so the result is <strong>False<\/strong> that is, <strong>tuple a is not smaller than tuple b<\/strong>.<\/li>\n\n\n\n<li><span style=\"text-decoration: underline\">Equal<\/span>: Since 5 is not equal to 4, <strong>tuple a is not equal to tuple b<\/strong>. Hence the result is <strong>False<\/strong>.<\/li>\n<\/ol>\n\n\n\n<p>This is how tuple comparison works in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using NumPy to compare two tuples in Python<\/h2>\n\n\n\n<p>The Python library <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy<\/a> can also be utilised to compare tuples in Python instead of using the comparison operators. NumPy supports function like <code>greater()<\/code>, <code>less()<\/code> and <code>equal()<\/code> to compare two collections of objects, elementwise. These functions can be used to perform comparison of tuples as demonstrated in the below example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = (2, 10, 3)\nb = (15, 10, 33)\n\nprint(&#039;Tuple a:&#039;, a, &#039;\\nTuple b:&#039;, b)\n\nprint(&#039;\\nElement-wise Comparison of the tuples using greater():&#039;, np.greater(a, b))\n\nprint(&#039;\\nElement-wise Comparison of the tuples using less():&#039;, np.less(a, b))\n\nprint(&#039;\\nElement-wise Comparison of the tuples using equal():&#039;, np.equal(a, b))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nTuple a: (2, 10, 3) \nTuple b: (15, 10, 33)\n\nElement-wise Comparison of the tuples using greater(): &#x5B;False False False]\n\nElement-wise Comparison of the tuples using less(): &#x5B; True False  True]\n\nElement-wise Comparison of the tuples using equal(): &#x5B;False  True False]\n<\/pre><\/div>\n\n\n<ol class=\"wp-block-list\">\n<li><span style=\"text-decoration: underline\">greater():<\/span> This function compares the corresponding pairs of elements. Since 2 is not greater than 15, 10 is not greater than 10 and 3 is not greater than 33, the result of each element-wise comparison of the two tuples is <strong>False<\/strong>.<\/li>\n\n\n\n<li><span style=\"text-decoration: underline\">less():<\/span> Here, 2&lt;15 and 3&lt;33 are <strong>True<\/strong> but as 10 is not less than 10, it is <strong>False<\/strong>.<\/li>\n\n\n\n<li><span style=\"text-decoration: underline\">equal():<\/span> Since only 10==10, the result of this comparison is <strong>True<\/strong> and 2 is not equal to 15 and 3 is not equal to 33, the result of both the element-wise comparisons is <strong>False<\/strong>.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Tuple comparison by eliminating duplicates<\/h2>\n\n\n\n<p>If a tuple contains duplicates, you can eliminate them and then perform tuple comparison.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = (1, 2, 5, 2, 3)\nb = (3, 2, 6, 1, 2)\n\nprint(&#039;Tuple a:&#039;, a, &#039;\\nTuple b:&#039;, b)\n\nnew_tuple_a = tuple(set(a))\nnew_tuple_b = tuple(set(b))\n\nprint(&#039;\\nNew Tuple a:&#039;, new_tuple_a, &#039;\\nNew Tuple b:&#039;, new_tuple_b)\n\nprint(&#039;\\nTuple a &gt; Tuple b?:&#039;, new_tuple_a&gt;new_tuple_b)\n\nprint(&#039;\\nTuple a &lt; Tuple b?:&#039;, new_tuple_a&lt;new_tuple_b)\n\nprint(&#039;\\nTuple a == Tuple b?:&#039;, new_tuple_a==new_tuple_b)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nTuple a: (1, 2, 5, 2, 3) \nTuple b: (3, 2, 6, 1, 2)\n\nNew Tuple a: (1, 2, 3, 5) \nNew Tuple b: (1, 2, 3, 6)\n\nTuple a &gt; Tuple b?: False\n\nTuple a &amp;lt; Tuple b?: True\n\nTuple a == Tuple b?: False\n<\/pre><\/div>\n\n\n<p>The duplicates in the tuples are eliminnated by first converting the tuples to another collection type called a set. The set only contains all the unique elements only once. Then these sets are type-casted back to tuple and the usual comparison is performed using the comparison operators. <\/p>\n\n\n\n<p>Since the first 3 elements in both the new tuples are equal here, the result of the comparison is decided on the basis of the last pair of corresponding elements. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Comparing tuples with heterogenous elements<\/h2>\n\n\n\n<p>As discussed earlier in this tutorial, tuples can be heterogenous i.e. contain elements of different data types. Such tuples can also be compared.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = (0, 2, &#039;a&#039;)\nb = (1, 5, &#039;d&#039;)\n\nprint(&#039;Tuple a:&#039;, a, &#039;\\nTuple b:&#039;, b)\n\nprint(&#039;\\nTuple a &gt; Tuple b?:&#039;, a&gt;b)\n\nprint(&#039;\\nTuple a &lt; Tuple b?:&#039;, a&lt;b)\n\nprint(&#039;\\nTuple a == Tuple b?:&#039;, a==b)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nTuple a: (0, 2, &#039;a&#039;) \nTuple b: (1, 5, &#039;d&#039;)\n\nTuple a &gt; Tuple b?: False\n\nTuple a &amp;lt; Tuple b?: True\n\nTuple a == Tuple b?: False\n<\/pre><\/div>\n\n\n<p>Since 0&lt;1 and 2&lt;5 is True and also the letter &#8216;a&#8217; is lexicographically smaller than the letter &#8216;d&#8217;, the <strong>tuple a is smaller than tuple b<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial you learnt how tuple comparison works in Python and also how to perform tuple comparison using Python and NumPy with the help of many examples. <\/p>\n\n\n\n<p>Please visit askpython.com for more such easy-to-understand Python tutorials.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple\" target=\"_blank\" rel=\"noreferrer noopener\">Python tuple<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/5292303\/how-does-tuple-comparison-work-in-python\" target=\"_blank\" rel=\"noreferrer noopener\">StackOverFlow<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python has four built-in data types that are used to store collections of data. These Python data types are: A tuple in Python is a collection of data items or objects stored inside a single variable. The data items inside a tuple are separated by commas. Syntactically, tuples store data inside round brackets i.e. (&lt;insert [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":42111,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-42107","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tuple"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/42107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=42107"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/42107\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/42111"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=42107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=42107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=42107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}