In Python, finding the intersection of two lists is a common operation. The **intersection** consists of the elements that are common to both lists. In sets, intersection always yields unique elements. When dealing with lists, it may be necessary to retain duplicate entries.

Let's go through ways to find the intersection by preserving duplicates and removing duplicates.

To find an intersection such that duplicates are preserved, traverse the first list, check the intersection, and remove one instance of that element from the second list. Element appended to intersection list must be removed from the second list to avoid a situation like lists having `[1,2,2]`

and `[1,2] `

to result in `[1,2,2]`

. On appending `2`

to intersection list we remove it from the second list so that repeated instance of `2`

from the first list is compared to the rest of the second list.

list1 = [1, 2, 3, 4, 4, '5',"educative"]list2 = [4,"educative", 4, '5', 6, 7, 8,"edu"]intersection = []for element in list1:if element in list2:intersection.append(element)list2.remove(element)print(intersection)

In this method, we iterate over each element in `list1`

. If the element is present in `list2`

, we append it to the `intersection`

list and remove it from `list2`

.

The time complexity of this method is

To find intersection such that duplicates are removed, we can either modify the code given above or use set operations after converting our lists to sets.

Let's first modify the given code.

list1 = [1, 2, 3, 4, 4, '5',"educative"]list2 = [4,"educative", 4, '5', 6, 7, 8,"edu"]intersection = []for element in list1:if element in list2 and element not in intersection:intersection.append(element)print(intersection)

In this modification, we iterate over each element in `list1`

. We check if the element is present in `list2`

and also ensure that it has not already been added to the `intersection`

list. If both conditions are satisfied, we append the element to the `intersection`

list.

The time complexity of this method is

We can find the intersection of lists such that duplicates are removed by converting them to sets, using set operations, and converting them back to sets.

**Using**`&`

**operator:**One way is to find the intersection of two lists by using the`&`

operator. This operator is overloaded for the set type in Python. Here's how we can find the intersection of two lists using the`&`

operator.

list1 = [1, 2, 3, 4, 4, '5',"educative"]list2 = [4,"educative", 4, '5', 6, 7, 8,"edu"]intersection = list(set(list1) & set(list2))print(intersection)

In this method, we convert both lists to sets using the `set()`

function, which eliminates duplicate elements. Then, we use the `&`

operator to perform the intersection operation on the sets. Finally, we convert the result back to a list using the `list()`

function.

**Using the**`intersection()`

**method:**Python sets have a built-in`intersection()`

method that can be used to find the common elements between two sets. We can use this method to find the intersection of two lists by converting them to sets. Here's an example.

list1 = [1, 2, 3, 4, 4, '5',"educative"]list2 = [4,"educative", 4, '5', 6, 7, 8,"edu"]intersection = list(set(list1).intersection(list2))print(intersection)

In this method, we convert both lists to sets using the `set()`

function and then apply the `intersection()`

method on one set while passing the other set as an argument. Finally, we convert the resulting set back to a list using the `list()`

function.

The time complexity of finding the intersection of lists using set operations is

Finding the intersection of two lists in Python can be achieved through various methods, by manipulating lists or converting them to sets. These approaches provide flexibility and efficiency depending on the specific requirements. By applying these techniques, we can easily extract the common elements between lists to perform further operations or analysis as required.

Copyright ©2024 Educative, Inc. All rights reserved

TRENDING TOPICS