This short Python tutorial shows how to get the difference, intersection and also the union of two lists. The video shows different options for the operation with a simple example. The first option uses list comprehension, the second uses set and the third option utilizes logical operators. The difference can be created with the following command:
[name for name in list1 if name not in list2]
list(set(list1).difference(set(list2)))
The union of two lists can be done with:
list(set(list1).union(set(list2)))
list(set(list1) | set(list2))
The intersection can be created with:
list(set(list1).intersection(set(list2)))
Many thanks for watching!
Негізгі бет Get difference between two lists in Python
Пікірлер