# 10 examples of 'find intersection of two lists python' in Python

Every line of 'find 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
``2def intersect(self, nums1, nums2):3  """4  :type nums1: List[int]5  :type nums2: List[int]6  :rtype: List[int]7  """8  ans = []9  nums1.sort()10  nums2.sort()11  i = j = 012  while i < len(nums1) and j < len(nums2):13    if nums1[i] < nums2[j]:14      i += 115    elif nums1[i] > nums2[j]:16      j += 117    else:18      ans.append(nums1[i])19      i += 120      j += 12122  return ans``
``94def intersect(self, nums1, nums2):95    """96    :type nums1: List[int]97    :type nums2: List[int]98    :rtype: List[int]99    """100    nums1.sort(), nums2.sort()  # Make sure it is sorted, doesn't count in time.101102    res = []103104    it1, it2 = 0, 0105    while it1 < len(nums1) and it2 < len(nums2):106        if nums1[it1] < nums2[it2]:107            it1 += 1108        elif nums1[it1] > nums2[it2]:109            it2 += 1110        else:111            res += nums1[it1],112            it1 += 1113            it2 += 1114115    return res``
``5def intersection(self, nums1, nums2):6    """7    :type nums1: List[int]8    :type nums2: List[int]9    :rtype: List[int]10    """11    if len(nums1) > len(nums2):12        return self.intersection(nums2, nums1)1314    lookup = set()15    for i in nums1:16        lookup.add(i)1718    res = []19    for i in nums2:20        if i in lookup:21            res += i,22            lookup.discard(i)2324    return res``
``226def getPairIntersection(set1,set2):227    acc = []228   # print set1,'+++',set2229    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``
``16def intersect(*lists):17	return list(reduce(set.intersection, (set(l) for l in lists)))``
``30def intersect(first_list, second_list):3132    return [x for x in first_list if x in second_list]``
``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 = list170        bigger = list271    else:72        smaller = list273        bigger = list1    74    75    if isinstance(bigger, dict):76        union_dict = bigger77    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``
``142def Intersection(A, B):143    """144    Returns the pixel count corresponding to the intersection145    between A and B.146    """147    C = A + B148    C[C != 2] = 0149    C[C == 2] = 1150    return C``
``55def mergeTwoLists2(self, l1, l2):56    """57    :param l1:58    :param l2:59    :return:6061    recursively62    """63    if not l1 or not l2:64        return l1 or l265    if l1.val < l2.val:66        l1.next = self.mergeTwoLists(l1.next, l2)67        return l168    else:69        l2.next = self.mergeTwoLists(l1, l2.next)70        return l2``
``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)``