## Intersection of two lists in numba

Related searches

I would like to know the fastest way to compute the intersection of two list within a numba function. Just for clarification: an example of the intersection of two lists:

```Input :
lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9]
lst2 = [9, 4, 5, 36, 47, 26, 10, 45, 87]
Output :
[9, 10, 4, 5]
```

The problem is, that this needs to be computed within the numba function and therefore e.g. sets can not be used. Do you have an idea? My current code is very basic. I assume that there is room for improvement.

```@nb.njit
def intersection:
result = []
for element1 in lst1:
for element2 in lst2:
if element1 == element2:
result.append(element1)
....
```

Since numba compiles and runs your code in machine code, your probably at the best for such a simple operation. I ran some benchmarks below

```@nb.njit
def loop_intersection(lst1, lst2):
result = []
for element1 in lst1:
for element2 in lst2:
if element1 == element2:
result.append(element1)
return result

@nb.njit
def set_intersect(lst1, lst2):
return set(lst1).intersection(set(lst2))
```

Resuls

```loop_intersection
40.4 µs ± 1.5 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

set_intersect
42 µs ± 6.74 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
```

How to find the intersection of two lists or the number of elements , There are lots of ways to find intersection of two given list simplest ways given below : Using set : List1=[1, 2,3,4,5,3,4] list2=[1, 5,4,2,8,2,3] Set1=set(List1)� def intersection(lst1, lst2): return list(set(lst1) & set(lst2)) then simply call the function intersection(lst1,lst2). This will be the easiest way.

I played with this a bit to try and learn something, realizing that the answer has already been given. When I run the accepted answer I get a return value of [9, 10, 5, 4, 9]. I wasn’t clear if the repeated 9 was acceptable or not. Assuming it’s OK, I ran a trial using list comprehension to see it made any difference. My results:

```from numba import jit

def createLists():
l1 = [15, 9, 10, 56, 23, 78, 5, 4, 9]
l2 = [9, 4, 5, 36, 47, 26, 10, 45, 87]

@jit
def listComp():
l1, l2 = createLists()
return [i for i in l1 for j in l2 if i == j]
```

%timeit listComp() 5.84 microseconds +/- 10.5 nanoseconds

Or if you can can use Numpy this code is even faster and removes the duplicate "9" and is much faster with the Numba signature.

```import numpy as np
from numba import jit, int64

@jit(int64[:](int64[:], int64[:]))
def JitListComp(l1, l2):
l3 = np.array([i for i in l1 for j in l2 if i == j])
return np.unique(l3) # and i not in crossSec]

@jit
def CreateList():
l1 = np.array([15, 9, 10, 56, 23, 78, 5, 4, 9])
l2 = np.array([9, 4, 5, 36, 47, 26, 10, 45, 87])
return JitListComp(l1, l2)

CreateList()
Out: array([ 4,  5,  9, 10])

%timeit CreateList()
1.71 µs ± 10.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
```

Python, Recommended Posts: Python | Intersection of multiple lists � Python | Program to count number of lists in a list of lists � Python | Zipping two lists of� Learn how to find the intersection point of two linked list. Example Input: 13 -> 4 -> 12 -> 27 -> null 14 -> 4 -> 82 -> 11 -> 12 -> 27 -> null Output: 12 An intersection point is an element present in both the linked list such that there is equal amount of elements after them.

You can use set operation for this:

```def intersection(lst1, lst2):
return list(set(lst1) & set(lst2))
```

then simply call the function `intersection(lst1,lst2)`. This will be the easiest way.

Supported Python features — Numba 0.52.0.dev0+42.g4c250bc , 0, Numba did not support the creation of nested lists. Numba also supports “array comprehension” that is a list comprehension followed immediately by a call to� Find the intersection of two arrays. Return the sorted, unique values that are in both of the input arrays. Parameters ar1, ar2 array_like. Input arrays. Will be flattened if not already 1D. assume_unique bool. If True, the input arrays are both assumed to be unique, which can speed up the calculation.

Listings — Numba 0.50.1 documentation, 3. Consider a for loop to accept values for the two lists. 4. Take the number of elements in the list and store it in a variable. Intersection of two given sorted linked lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory — the original lists should not be changed.

Python Program to Find the Intersection of Two Lists, code. # Python program to illustrate the intersection. # of two lists in most simple way. def intersection (lst1, lst2): lst3 = [value for value in lst1 if value in lst2] return lst3. # Driver Code. lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69] lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26]

Find intersection of two lists with formula. The below formula can help you find intersection of two lists in Excel. Please do as follows. 1. Select a blank column, copy formula =IF(ISERROR(MATCH(B2,\$C\$2:\$C\$9,0)),"",B2) into the Formula Bar and then press the Enter key. See screenshot:

• `numba` supports sets, but only if they contain objects of a single type - which in your case they do? numba.pydata.org/numba-doc/dev/reference/pysupported.html