6

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).

Hamman Samuel
  • 2,350
  • 4
  • 30
  • 41

6 Answers6

11

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))
Roel Schroeven
  • 1,778
  • 11
  • 12
  • 1
    Thanks to everyone for the the excellent answers, I chose this as the best because of it's conciseness and use of examples – Hamman Samuel Jul 03 '15 at 12:12
7

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.

Community
  • 1
  • 1
jayelm
  • 7,236
  • 5
  • 43
  • 61
4

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.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
2

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]
  • I suggest making sure your code block is indented by _exactly_ 4 spaces, as any more will cause the Python interpreter to complain about unexpected indent if the code is copy-pasted into it (which is the case right now). – celticminstrel Jul 03 '15 at 16:20
  • To me, it's returning this results don't know why: [True, False, False, False, False, False, False, False, False] – Hardik Raval Aug 10 '21 at 07:24
  • import itertools lst1 = [ 1,2,3 ] lst2 = [ 1,4,6 ] lst3 = list(map(lambda x: x[0] == x[1] , itertools.product(lst1,lst2))) print(lst3) – Hardik Raval Aug 10 '21 at 07:25
-2

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
Jiangtang Hu
  • 127
  • 1
  • 3