7 examples of 'intersection of two lists python' in Python

Every line of 'intersection of two lists python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
506def intersection_update(self, other):
507 """Update self to include only the intersection with other."""
508 other = set(other)
509 indices_to_delete = set()
510 for i, elem in enumerate(self):
511 if elem not in other:
512 indices_to_delete.add(i)
513 if indices_to_delete:
514 self._delete_values_by_index(indices_to_delete)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

61def union(list1, list2):
62 if list1 is None:
63 list1 = {}
64 if list2 is None:
65 list2 = {}
66
67 # (Order matters slightly so that has_key is called fewer times)
68 if len(list1) < len(list2):
69 smaller = list1
70 bigger = list2
71 else:
72 smaller = list2
73 bigger = list1
74
75 if isinstance(bigger, dict):
76 union_dict = bigger
77 else:
78 union_dict = {}
79 for e in bigger:
80 union_dict[e] = bigger[e]
81 for e in smaller:
82 union_dict[e] = smaller[e]
83 return union_dict
16def intersect(*lists):
17 return list(reduce(set.intersection, (set(l) for l in lists)))
30def intersect(first_list, second_list):
31
32 return [x for x in first_list if x in second_list]
226def getPairIntersection(set1,set2):
227 acc = []
228 # print set1,'+++',set2
229 for el1,el2 in zip(set1,set2):
230 temp2 = []
231 temp2.append(tuple([val for val in el1[0] if val in el2[0]]))
232 for el1x,el2x in zip(el1[1],el2[1]):
233 temp2.append(([val for val in el1x if val in el2x],))
234 acc.append(tuple(temp2))
235 return acc
142def zip(self, other: Iterable[B]) -> Iterable[Tuple[A, B]]:
143 """
144 Zip together with another iterable
145
146 Example:
147 >>> List(List(range(2)).zip(range(2)))
148 [(0, 0), (1, 1)]
149
150 Args:
151 other: Iterable to zip with
152 Return:
153 Zip with ``other``
154 """
155 return zip(self, other)
181def mergeList(list_of_list):
182 return list(itertools.chain.from_iterable(list_of_list))

Related snippets