{"id":6626,"date":"2023-11-05T10:28:04","date_gmt":"2023-11-05T04:58:04","guid":{"rendered":"https:\/\/python-programs.com\/?p=6626"},"modified":"2023-11-10T12:18:50","modified_gmt":"2023-11-10T06:48:50","slug":"compare-and-get-differences-between-two-lists-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/compare-and-get-differences-between-two-lists-in-python\/","title":{"rendered":"Compare and get Differences between two Lists in Python"},"content":{"rendered":"
Lists in Python:<\/strong><\/p>\n Lists are the most versatile ordered list object type in Python. It’s also known as a sequence, which is an ordered group of objects that can contain objects of any data form, including Python Numbers, Python Strings, and nested lists. One of the most widely used and flexible Python Data Types is the list.<\/p>\n You can check if two lists are equal python<\/a>.<\/span><\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Let’s say we have two lists<\/p>\n There may be certain items in the first list that are not present in the second list. There are also several items that are present in the second list but not in the first list. We’d like to compare our two lists to figure out what the variations are.<\/p>\n There are several ways to compare and get differences between two lists some of them are:<\/p>\n When we make a set from a list, it only includes the list’s unique elements. So, let’s transform our lists into sets, and then subtract these sets to find the differences. Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Instead of subtracting two sets with the \u2013 operator in the previous solution, we can get the differences by using the set\u00a0difference() feature.<\/p>\n So, let’s convert our lists to sets, and then use the difference() function to find the differences between two lists.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n To find the differences, we can iterate over both lists and search for elements in other lists. However, we can use list comprehension for iteration.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We had all of the variations between two lists in two steps in all of the previous solutions. Using symmetric difference(), however, we can accomplish this in a single stage. Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Quick approach for solving this problem is to use ^ and sets.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Lists in Python: Lists are the most versatile ordered list object type in Python. It’s also known as a sequence, which is an ordered group of objects that can contain objects of any data form, including Python Numbers, Python Strings, and nested lists. One of the most widely used and flexible Python Data Types is …<\/p>\nlist1 = [\"Hello\", \"Geeks\", \"this\", \"is\", \"BTechGeeks\", \"online\", \"Platform\"]\r\nlist2 = [\"Hello\", \"world\", \"this\", \"Python\", \"Coding\", \"Language\"]<\/pre>\n
Printing the Differences between the lists : \r\n['Geeks', 'is', 'BTechGeeks', 'online', 'Platform', 'world', 'Python', 'Coding', 'Language']<\/pre>\n
Compare and get Differences between two Lists in Python<\/h2>\n
\n
Method #1:Using union function in sets<\/h3>\n
\nWe discovered the variations between the two lists, i.e. elements that are present in one list but not in the other.<\/p>\n# given two lists\r\nlist1 = [\"Hello\", \"Geeks\", \"this\", \"is\", \"BTechGeeks\", \"online\", \"Platform\"]\r\nlist2 = [\"Hello\", \"world\", \"this\", \"Python\", \"Coding\", \"Language\"]\r\n# converting two lists to sets\r\nsetlist1 = set(list1)\r\nsetlist2 = set(list2)\r\n# getting the differences in both lists\r\nlistDif = (setlist1 - setlist2).union(setlist2 - setlist2)\r\nprint('Printing the Differences between the lists : ')\r\nprint(listDif)\r\n<\/pre>\n
Printing the Differences between the lists : \r\n['Geeks', 'is', 'BTechGeeks', 'online', 'Platform', 'world', 'Python', 'Coding', 'Language']<\/pre>\n
Method #2:Using set.difference()<\/h3>\n
# given two lists\r\nlist1 = [\"Hello\", \"Geeks\", \"this\", \"is\", \"BTechGeeks\", \"online\", \"Platform\"]\r\nlist2 = [\"Hello\", \"world\", \"this\", \"Python\", \"Coding\", \"Language\"]\r\n# converting two lists to sets\r\nsetlist1 = set(list1)\r\nsetlist2 = set(list2)\r\n# getting elements in first list which are not in second list\r\ndifference1 = setlist1.difference(setlist2)\r\n# getting elements in second list which are not in first list\r\ndifference2 = setlist2.difference(setlist1)\r\nlistDif = difference1.union(difference2)\r\nprint('Printing the Differences between the lists : ')\r\nprint(listDif)\r\n<\/pre>\n
Printing the Differences between the lists : \r\n['Geeks', 'is', 'BTechGeeks', 'online', 'Platform', 'world', 'Python', 'Coding', 'Language']<\/pre>\n
Method #3:Using List Comprehension<\/h3>\n
# given two lists\r\nlist1 = [\"Hello\", \"Geeks\", \"this\", \"is\", \"BTechGeeks\", \"online\", \"Platform\"]\r\nlist2 = [\"Hello\", \"world\", \"this\", \"Python\", \"Coding\", \"Language\"]\r\n\r\n# getting elements in first list which are not in second list\r\ndifference1 = [element for element in list1 if element not in list2]\r\n# getting elements in second list which are not in first list\r\ndifference2 = [element for element in list2 if element not in list1]\r\nlistDif = difference1+difference2\r\nprint('Printing the Differences between the lists : ')\r\nprint(listDif)\r\n<\/pre>\n
Printing the Differences between the lists : \r\n['Geeks', 'is', 'BTechGeeks', 'online', 'Platform', 'world', 'Python', 'Coding', 'Language']<\/pre>\n
Method #4:Using set.symmetric_difference()<\/h3>\n
\nSet has a member function called symmetric difference() that takes another sequence as an argument. It returns a new set containing elements from either the calling set object or the sequence statement, but not both. In other words, it returns the differences between set and list. Let’s see if we can use this to determine the differences between two lists.<\/p>\n# given two lists\r\nlist1 = [\"Hello\", \"Geeks\", \"this\", \"is\", \"BTechGeeks\", \"online\", \"Platform\"]\r\nlist2 = [\"Hello\", \"world\", \"this\", \"Python\", \"Coding\", \"Language\"]\r\n\r\nlistDif = set(list1).symmetric_difference(list2)\r\nprint('Printing the Differences between the lists : ')\r\nprint(listDif)\r\n<\/pre>\n
Printing the Differences between the lists : \r\n{'is', 'online', 'world', 'BTechGeeks', 'Python', 'Language', 'Coding', 'Geeks', 'Platform'}<\/pre>\n
Method #5:Using set and ^ operator<\/h3>\n
# given two lists\r\nlist1 = [\"Hello\", \"Geeks\", \"this\", \"is\", \"BTechGeeks\", \"online\", \"Platform\"]\r\nlist2 = [\"Hello\", \"world\", \"this\", \"Python\", \"Coding\", \"Language\"]\r\n\r\nlistDif = set(list1) ^ set(list2)\r\nprint('Printing the Differences between the lists : ')\r\nprint(listDif)\r\n<\/pre>\n
Printing the Differences between the lists : \r\n{'is', 'online', 'world', 'BTechGeeks', 'Python', 'Language', 'Coding', 'Geeks', 'Platform'}<\/pre>\n","protected":false},"excerpt":{"rendered":"