This article shows how to compare two lists.
Contents
- How to use "set( )" function for comparing list elements
- Check if the two lists match
- Get elements common to the two lists
- Get elements that are not common to the two lists (differences)
- [Appendix] Compare lists with "for loop" and "in" operator
sponsored link
How to use "set( )" function for comparing list elements
In the beginning, I show how to use "set( )" function for comparing list elements.
The set type is a data type that deals with set.
Like a list, a set consists of multiple elements, but is in no particular order.
Also, each element of the set type does not duplicate.
Using these features of set, elements can be compared.
The set( ) function can be used to convert from a list type to a set type.
Here is an example code for conversion from list type to set type
fruits_1 = ["Apple", "Banana", "Orange", "Banana"]
print(fruits_1)
print(type(fruits_1))
# ['Apple', 'Banana', 'Orange', 'Banana']
# <class 'list'>
set_fruits_1 = set(fruits_1)
print(set_fruits_1)
print(type(set_fruits_1))
# {'Orange', 'Banana', 'Apple'}
# <class 'set'>
Duplicate elements of list type are eliminated.
The elements of set are displayed in different order than list, because set is in no particular order.
sponsored link
Check if the two lists match
I show how to check if the two lists match.
To compare two lists, use "==" and "! =" operators.
By comparing lists with the "== operator", True is returned only if the value and order of the elements match.
fruits_1 = ["Apple", "Banana", "Orange"]
fruits_2 = ["Apple", "Banana", "Orange"]
fruits_3 = ["Banana", "Orange", "Apple"]
print(fruits_1 == fruits_2)
# True
print(fruits_1 == fruits_3)
# False
To compare only the elements of a list, use "set()" function.
fruits_1 = ["Apple", "Banana", "Orange"]
fruits_2 = ["Apple", "Banana", "Orange"]
fruits_3 = ["Banana", "Orange", "Apple"]
print(set(fruits_1) == set(fruits_2))
# True
print(set(fruits_1) == set(fruits_3))
# True
sponsored link
Get elements common to the two lists
I show how to get elements common to the two lists.
By taking "and" of set with "& operator," you can get the elements in common.
Here is an example code.
fruits_1 = ["Apple", "Banana", "Orange", "Apple", "Lemon"]
fruits_2 = ["Grape", "Apple", "Lemon"]
and_list = set(fruits_1) & set(fruits_2)
print(and_list)
# {'Apple', 'Lemon'}
print(list(and_list))
# ['Apple', 'Lemon']
sponsored link
Get elements that are not common to the two lists (differences)
I show how to get elements that are not common to the two lists.
By taking "XOR" of set with "^ operator," you can get the elements not in common.
Here is an example code.
fruits_1 = ["Apple", "Banana", "Orange", "Apple", "Lemon"]
fruits_2 = ["Grape", "Apple", "Lemon"]
diff_list = set(fruits_1) ^ set(fruits_2)
print(diff_list)
# {'Banana', 'Orange', 'Grape'}
print(list(diff_list))
# ['Banana', 'Orange', 'Grape']
sponsored link
[Appendix] Compare lists with "for loop" and "in" operator
The "for loop" and "in operator" can also be used to compare list elements.
In the following sample code, duplicate elements of the list are also retrieved.
fruits_1 = ["Apple", "Banana", "Orange", "Apple", "Lemon"]
fruits_2 = ["Grape", "Apple", "Lemon"]
match_list = []
for elem in fruits_1 :
#print(elem)
if elem in fruits_2 :
match_list.append(elem)
print(match_list)
['Apple', 'Apple', 'Lemon']
sponsored link