7

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
  • This doesn't work as I expect Commented Jul 3, 2015 at 12:12

6 Answers 6

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))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to everyone for the the excellent answers, I chose this as the best because of it's conciseness and use of examples
8

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 Comments

itertools.product is nice, too.
@bereal didn't know about that one, nice suggestion.
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.

6 Comments

Out of curiosity, what does map return in Python 3 that you have to add the explicit casting?
Question seems to be asking for a list with 9 elements (all possible combinations), not three?
The OP wants to compare each element of lst1 with each element of lst2, so should be 9.
@JesseMu Sorry read the question wrongly, have fixed it to use itertools.product instead of zip
@SuperBiasedMan zip returns an iterator in Python 3 , not a list.
|
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 Comment

Thanks for pointing out the correct terminology, this is a product operation
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 Comments

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).
To me, it's returning this results don't know why: [True, False, False, False, False, False, False, False, False]
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)
-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.