Compare Each Item of Two Lists in Python

Compare Each Item of Two Lists in Python

For example, suppose list_1 = [a,b,c] and list_2 = [m,n,o]. I want to compare each item from one list to the other, for example, create an output list such that

[a == m, a == n, a == o, b == m, b == n, b == o...]

For simplicity I'm using the == operation but this could also be a summation, e.g.

[a + m, a + n, a + o, b + m...]

I know how I could do this with two loops, but I'm wondering a lambda function with map() or list comprehensions could be used? I've searched online but only found one-to-one comparisons of list items, e.g. map(lambda x,y: x + y, list_1, list_2).

1

6 Answers

itertools.product() can generate all combinations for you:

import itertools
list_1 = [1,5,4]
list_2 = [2,3,4]
# using list comprehensions
comparisons = [a == b for (a, b) in itertools.product(list_1, list_2)]
sums = [a + b for (a, b) in itertools.product(list_1, list_2)]
# using map and lambda
comparisons = map(lambda (a, b): a == b, itertools.product(list_1, list_2))
sums = map(lambda (a, b): a + b, itertools.product(list_1, list_2))
1

To get all the permutations of elements with a list comprehension:

[a == b for a in list_1 for b in list_2]

Functionality is the same as the nested for loops:

list_3 = []
for a in list_1:
    for b in list_2:
        list_3.append(a == b)  # Or a + b, etc.

Functional implementation is a bit more confusing:

list_3 = map(lambda x: map(lambda y: y == x, list_2), list_1)

This creates a list of lists, however, so you'd want to flatten it with any of the techniques described here

sum(list_3, [])

Or use itertools.product as suggested by @bereal.

2

For python 3.x - Yes you can do this with map function and itertools.product function and lambda expression -

>>> lst1 = [1,5,4]
>>> lst2 = [2,3,4]
>>> lst3 = list(map(lambda x: x[0] == x[1] , itertools.product(lst1,lst2)))
>>> lst3
[False, False, False, False, False, False, False, False, True]

For Python 2.x you can use same expression, just map function in Python 2.x returns a list so the list(...) is not required.

6

You want to apply an operator on pairs from the cartesian product of two lists. Let's say op defines the operation you want to apply to the two items, e.g:

op = lambda x, y: x == y

and have two lists

a = [1, 2, 3]
b = [2, 3, 4]

You can apply op on all pairs as a list comprehension as follows:

c = [op(x, y) for y in b for x in a]

To use the map function you first need to create the cartesian product using itertools.product. This effectively creates a double loop over the elements of both list just like the list comprehension. You will need to adjust the op definition slightly for this since it will only receive one argument consisting of the tuple (x, y). For example:

op2 = lambda t: t[0] == t[1]
d = map(op2, itertools.product(a, b))
1

You can do this:

list_1 = [1,2,3]
list_2 = [1,4,6]
is_it_equal_function = lambda x:x in list_1
is_it_equal_list = map(is_it_equal_function, list_2)

It will return:

[True, False, False]
3

If we like the idea of vectorization, we could try numpy:

import numpy as np
list_1 = ['a','b','c']
list_2 = ['m','n','o']
l1 = np.array(list_1)
l2 = np.array(list_2)
l1 == l2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.