{"id":1490,"date":"2022-12-28T20:00:00","date_gmt":"2022-12-28T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=1490"},"modified":"2022-10-23T14:40:11","modified_gmt":"2022-10-23T05:40:11","slug":"list-comparison","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/list-comparison\/","title":{"rendered":"[Python] How to Compare two Lists"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to compare two lists<\/span>.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Contents<\/span><\/p><div class=\"free-inbox\">\n<p><\/p>\n\n\n\n<ul><li>How to use &#8220;set( )&#8221; function for comparing list elements<\/li><li>Check if the two lists match<\/li><li>Get elements common to the two lists<\/li><li>Get elements that are not common to the two lists (differences)<\/li><li>[Appendix] Compare lists with &#8220;for loop&#8221; and &#8220;in&#8221; operator<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How to use &#8220;set( )&#8221; function for comparing list elements<\/h2>\n\n\n\n<p>In the beginning, I show <span class=\"st-mymarker-s\">how to use &#8220;set( )&#8221; function for comparing list elements<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The set type is a data type that deals with set.<\/p>\n\n\n\n<p>Like a list, a set consists of multiple elements, but  is in no particular order.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Also, each element of the set type does not duplicate.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Using these features of set, elements can be compared.<\/p>\n\n\n\n<p>The set( ) function can be used to convert from a list type to a set type.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code for conversion from list type to set type<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits_1 = &#091;\"Apple\", \"Banana\", \"Orange\", \"Banana\"]\n\nprint(fruits_1)\nprint(type(fruits_1))\n# &#091;'Apple', 'Banana', 'Orange', 'Banana']\n# &lt;class 'list'&gt;\n\nset_fruits_1 = set(fruits_1)\nprint(set_fruits_1)\nprint(type(set_fruits_1))\n# {'Orange', 'Banana', 'Apple'}\n# &lt;class 'set'&gt;<\/code><\/pre>\n\n\n\n<p>Duplicate elements of list type are eliminated.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The elements of set are displayed in different order than list, because set is in no particular order.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Check if the two lists match<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to check if the two lists match<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>To compare two lists, use &#8220;==&#8221; and &#8220;! =&#8221; operators.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>By comparing lists with the &#8220;== operator&#8221;, True is returned only if the value and order of the elements match.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits_1 = &#091;\"Apple\", \"Banana\", \"Orange\"]\nfruits_2 = &#091;\"Apple\", \"Banana\", \"Orange\"]\nfruits_3 = &#091;\"Banana\", \"Orange\", \"Apple\"]\n\nprint(fruits_1 == fruits_2)\n# True\nprint(fruits_1 == fruits_3)\n# False<\/code><\/pre>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>To compare only the elements of a list, use &#8220;set()&#8221; function.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits_1 = &#091;\"Apple\", \"Banana\", \"Orange\"]\nfruits_2 = &#091;\"Apple\", \"Banana\", \"Orange\"]\nfruits_3 = &#091;\"Banana\", \"Orange\", \"Apple\"]\n\nprint(set(fruits_1) == set(fruits_2))\n# True\nprint(set(fruits_1) == set(fruits_3))\n# True<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Get elements common to the two lists<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to get elements common to the two lists.<\/span><\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>By taking &#8220;and&#8221; of set with &#8220;&amp; operator,&#8221; you can get the elements in common.<\/p>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits_1 = &#091;\"Apple\", \"Banana\", \"Orange\", \"Apple\", \"Lemon\"]\nfruits_2 = &#091;\"Grape\", \"Apple\", \"Lemon\"]\n\nand_list = set(fruits_1) &amp; set(fruits_2)\nprint(and_list)\n# {'Apple', 'Lemon'}\n\nprint(list(and_list))\n# &#091;'Apple', 'Lemon']<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Get elements that are not common to the two lists (differences)<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to get elements that are not common to the two lists.<\/span><\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>By taking &#8220;XOR&#8221; of set with &#8220;^ operator,&#8221; you can get the elements not in common.<\/p>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits_1 = &#091;\"Apple\", \"Banana\", \"Orange\", \"Apple\", \"Lemon\"]\nfruits_2 = &#091;\"Grape\", \"Apple\", \"Lemon\"]\n\ndiff_list = set(fruits_1) ^ set(fruits_2)\nprint(diff_list)\n# {'Banana', 'Orange', 'Grape'}\nprint(list(diff_list))\n# &#091;'Banana', 'Orange', 'Grape']<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">[Appendix] Compare lists with &#8220;for loop&#8221; and &#8220;in&#8221; operator<\/h2>\n\n\n\n<p>The &#8220;for loop&#8221; and &#8220;in operator&#8221; can also be used to compare list elements.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In the following sample code, duplicate elements of the list are also retrieved.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits_1 = &#091;\"Apple\", \"Banana\", \"Orange\", \"Apple\", \"Lemon\"]\nfruits_2 = &#091;\"Grape\", \"Apple\", \"Lemon\"]\nmatch_list = &#091;]\n\nfor elem in fruits_1 :\n    #print(elem)\n    if elem in fruits_2 :\n    match_list.append(elem)\nprint(match_list)\n&#091;'Apple', 'Apple', 'Lemon']<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-matched-content-rows-num=\"4,2\"\n     data-matched-content-columns-num=\"1,4\"\n     data-matched-content-ui-type=\"image_stacked,image_stacked\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"2243394422\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to compare two lists. How to use &#8220;set( )&#8221; function for comparing  &#8230; <\/p>\n","protected":false},"author":1,"featured_media":2276,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,11],"tags":[16,19],"_links":{"self":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1490"}],"collection":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/comments?post=1490"}],"version-history":[{"count":61,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1490\/revisions"}],"predecessor-version":[{"id":2419,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1490\/revisions\/2419"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/2276"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=1490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=1490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=1490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}