I have tried for a while but can't find a simple way to join 2 lists or arrays based only on common values. Similar to an SQL inner join but with arrays/lists and not dict, or some other data type. eg.
a = [1, 2, 3]
b = [2, 3, 4]
join(a, b)
prints
[2, 3]
seems so simple but lacking from python or numpy.
Method 2:Using Set's intersection property Convert the list to set by conversion. Use the intersection function to check if both sets have any elements in common. If they have many elements in common, then print the intersection of both sets.
Use the sum() function to concatenate nested lists to a single list by passing an empty list as a second argument to it.
defaultdict and traverse through 'lst1'+'lst2' and append the first element of 'lst1' as key and tupled second element of both respective sublists as value. Finally, we traverse through 'dict1' and initialize 'dictlist' with the desired output.
Probably a duplicate, but in case it is not:
>>> a = [1,2,3]
>>> b = [2,3,4]
>>> list(set(a) & set(b))
[2, 3]
For large lists (external data), see this S.O. answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With