{"id":3630,"date":"2021-08-18T11:28:50","date_gmt":"2021-08-18T05:58:50","guid":{"rendered":"https:\/\/python-programs.com\/?p=3630"},"modified":"2021-11-22T18:39:34","modified_gmt":"2021-11-22T13:09:34","slug":"python-check-if-two-lists-are-equal","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-check-if-two-lists-are-equal\/","title":{"rendered":"Python: Check if Two Lists are Equal | How do you Check if a List is the Same as Another List Python?"},"content":{"rendered":"
In Order to check if two lists or identical or not we need to check if two unordered lists have similar elements in the exact similar position.\u00a0 You might need this in your day-to-day programming. Go through the tutorial over here and determine if two lists are equal or not in Python easily.<\/p>\n
We have mentioned the various techniques for finding if a list is the same as the Another or Not along with sample programs. Use them as a guide for resolving your doubts if any on Comparing Two Lists and finding whether they are identical or not. You can also find difference between two lists python<\/a> by going through our other tutorials.<\/p>\n Multiple items may be stored in a single variable using lists. Square brackets[] are used to create a list. Lists are one of four built-in Python storage types for storing sets of data; the other three are Tuple, Set, and Dictionary, all of which have different qualities and applications.<\/p>\n Comparing lists irrespective of the order of lists:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Comparing list based on the order of lists:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to check if two lists are equal or not. We have outlined some of them so that you can choose the method that you are comfortable with and determine if the two lists are equal or not. They are explained in the following way<\/p>\n Check lists irrespective of order of elements<\/strong><\/p>\n We start by sorting the list so that if both lists are similar, the elements are in the same place. However, this ignores the order of the elements in the list. So, by comparing sorting versions of lists, we can determine whether or not they are equal.<\/p>\n Below is the implementation:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n We can normally get the frequency of each variable in a list using Counter(), and we can search for it in both lists to see whether they are similar or not. However, this approach disregards the order of the elements in the list and only considers the frequency of the elements.<\/p>\n Below is the implementation:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n From our lists, we can generate two sorted numpy arrays, which we can compare using numpy.array equal() to see if they contain the same elements.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Checking lists based on of order of elements<\/strong><\/p>\n The == operator can be used to compare two lists directly. If both lists are exactly identical, it will return True; otherwise, it will return False.<\/p>\n Below is the implementation:<\/strong><\/p>\n <\/p>\n\n
<\/a>List – Definition<\/h2>\n
<\/a>Examples for Checking if Two Lists are Equal or Not<\/h3>\n
firstlist = ['hello', 'this', 'is', 'BTechGeeks'] \r\n\r\nsecondlist = ['this', 'is', 'BTechGeeks','hello']<\/pre>\n
Both lists are equal<\/pre>\n
firstlist = ['hello', 'this', 'is', 'BTechGeeks','is'] \r\n\r\nsecondlist = ['this', 'is', 'BTechGeeks','hello','the']<\/pre>\n
Both lists are not equal<\/pre>\n
firstlist = ['this' ,'is','BTechGeeks']<\/span>\r\nsecondlist = ['this' ,'is','BTechGeeks']<\/span><\/pre>\n
Both lists are equal<\/pre>\n
firstlist = ['hello', 'this' ,'is','BTechGeeks']<\/span>\r\nsecondlist = ['this' ,'is','BTechGeeks']<\/span><\/pre>\n
Both lists are not equal<\/pre>\n
<\/a>How to Compare if Two Lists are Identical or Not?<\/h2>\n
\n
<\/a>Method #1: Using sorting()<\/h3>\n
# function to check both lists if they are equal\r\ndef checkList(firstlist, secondlist):\r\n # sorting the lists\r\n firstlist.sort()\r\n secondlist.sort()\r\n # if both the lists are equal the print yes\r\n if(firstlist == secondlist):\r\n print(\"Both lists are equal\")\r\n else:\r\n print(\"Both lists are not equal\")\r\n\r\n\r\n# Driver code\r\n# given two lists\r\nfirstlist = ['hello', 'this', 'is', 'BTechGeeks']\r\nsecondlist = ['this', 'is', 'BTechGeeks','hello']\r\n# passing both the lists to checklist function\r\ncheckList(firstlist, secondlist)\r\n<\/pre>\n
Both lists are not equal<\/pre>\n
<\/a>Method #2: Using Counter() function<\/h3>\n
# importing counter function from collections\r\nfrom collections import Counter\r\n\r\n# function to check both lists if they are equal\r\ndef checkList(firstlist, secondlist):\r\n # Getting frequencies of both lists\r\n firstfreq = Counter(firstlist)\r\n secondfreq = Counter(secondlist)\r\n # if both the lists are equal the print yes\r\n if(firstfreq == secondfreq):\r\n print(\"Both lists are equal\")\r\n else:\r\n print(\"Both lists are not equal\")\r\n\r\n\r\n# Driver code\r\n# given two lists\r\nfirstlist = ['hello', 'this', 'is', 'BTechGeeks'] \r\nsecondlist = ['this', 'is', 'BTechGeeks','hello']\r\n# passing both the lists to checklist function\r\ncheckList(firstlist, secondlist)\r\n<\/pre>\n
Both lists are not equal<\/pre>\n
<\/a>Method #3: Using np.array_equal()<\/h3>\n
# importing numpy\r\nimport numpy\r\n# function to check both lists if they are equal\r\ndef checkList(firstlist, secondlist):\r\n # Convert both lists to sorted numpy arrays and compare them to see if they're equal.\r\n if(numpy.array_equal(numpy.array(firstlist).sort(), numpy.array(secondlist).sort())):\r\n print(\"Both lists are equal\")\r\n else:\r\n print(\"Both lists are not equal\")\r\n\r\n\r\n# Driver code\r\n# given two lists\r\nfirstlist\u00a0=\u00a0['hello',\u00a0'this',\u00a0'is',\u00a0'BTechGeeks']\u00a0\r\nsecondlist\u00a0=\u00a0['this',\u00a0'is',\u00a0'BTechGeeks','hello']\r\n# passing both the lists to checklist function\r\ncheckList(firstlist, secondlist)\r\n<\/pre>\n
Both lists are not equal<\/pre>\n
<\/a>Method #4: Using ‘=’ Operator<\/h3>\n
# function to check if both the lists are same\r\ndef checkList(firstlist, secondlist):\r\n # if both the lists are equal the print yes\r\n if(firstlist == secondlist):\r\n print(\"Both lists are equal\")\r\n else:\r\n print(\"Both lists are not equal\")\r\n\r\n\r\n# Driver code\r\n# given two lists\r\nfirstlist = ['hello', 'this', 'is', 'BTechGeeks']\r\nsecondlist = ['hello', 'this', 'is', 'BTechGeeks']\r\n# passing both the lists to checklist function\r\ncheckList(firstlist, secondlist)\r\n<\/pre>\n