Here is a Python program to find the intersection of two lists:
list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] intersection = list(set(list1) & set(list2)) print("Intersection of list1 and list2:", intersection)
In this program, we first define two lists list1
and list2
. Then, we use the built-in set
function to convert each list into a set, which removes any duplicates in the list. We then use the &
operator to find the intersection of the two sets. Finally, we convert the resulting set back to a list using the list
function and assign it to the variable intersection
.
We then print out the intersection of the two lists using the print
statement. When we run the program, the output will be:
Intersection of list1 and list2: [4, 5]
This means that the intersection of list1
and list2
contains the values 4 and 5, which are the common elements in both lists.
Method – 1: Using for loop:
Yes, we can also find the intersection of two lists using a for
loop in Python. Here is an example program that uses a for
loop to find the intersection of two lists:
list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] intersection = [] for element in list1: if element in list2: intersection.append(element) print("Intersection of list1 and list2:", intersection)
In this program, we first define two lists list1
and list2
. We then create an empty list called intersection
to store the common elements between the two lists.
We use a for
loop to iterate through each element in list1
. Inside the loop, we use an if
statement to check whether the current element is also in list2
. If it is, we append the element to the intersection
list.
Finally, we print out the intersection
list using the print
statement. When we run the program, the output will be:
Intersection of list1 and list2: [4, 5]
This means that the intersection of list1
and list2
contains the values 4 and 5, which are the common elements in both lists.
Method – 2: Convert List to Set:
Yes, another way to find the intersection of two lists in Python is by converting them into sets and using the intersection
method. Here’s an example program that uses this method:
list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] set1 = set(list1) set2 = set(list2) intersection = list(set1.intersection(set2)) print("Intersection of list1 and list2:", intersection)
In this program, we first define two lists list1
and list2
. We then convert them into sets using the built-in set
function and assign them to the variables set1
and set2
.
We then use the intersection
method to find the common elements between the two sets. This method returns a new set that contains only the elements that are common to both sets.
Finally, we convert the resulting set back to a list using the list
function and assign it to the variable intersection
. We then print out the intersection
list using the print
statement. When we run the program, the output will be:
Intersection of list1 and list2: [4, 5]
This means that the intersection of list1
and list2
contains the values 4 and 5, which are the common elements in both lists.