Compare each item of two lists in Python
python compare two lists element wise
python compare list elements
python compare two lists equal
python, how to compare two lists and get all indexes of matches
compare two lists in python and return the difference
how to compare two lists in python and return non matches
compare values within list 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).
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))
Compare each item of two lists in Python, itertools.product() can generate all combinations for you: import itertools list_1 = [1,5,4] list_2 = [2,3,4] # using list comprehensions comparisons Python Difference Between Two Lists. Lists in Python can be performed in different ways, but it depends on the outcome required. Two popular methods of comparison are set() and cmp(). The set() function creates an object that is a set object. The cmp() function is used to compare two elements or lists and return a value based on the arguments passed.
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
itertools.product as suggested by @bereal.
How to do a comparison of two lists in Python with each value, So your lists contains sets as elements. Loop through two list using nested loops and compare list items (which are sets here ) using equality ( == ) operator . I'm trying to compare two lists of integers, each the same size, in Python 2.6. The comparison I need is to compare the first item in List 1 with the first item in List 2, the second item in List 1 with the second item in List 2, and so on, and returns a result if ALL of the list items follow the same comparison criteria. It should behave as
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 == x , 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.
Python, Using Counter() , we usually are able to get frequency of each element in list, of 1 if both the index in two lists have equal elements, and then compare that This answer has good algorithmic performance, as only one of the lists (shorter should be preferred) is turned into a set for quick lookup, and the other list is traversed looking up its items in the set. – u0b34a0f6ae Sep 7 '09 at 12:08. bool (set (a).intersection (b)) for True or False – Akshay Oct 20 '17 at 3:20.
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 == t d = map(op2, itertools.product(a, b))
Python, In this method, we use the basic combination technique to copy elements from both the list with a regular check if one is present in the other or not. filter_none. edit 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]
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]
Difference Between Two Lists using Python Set & Without Set, Other non-set methods compare two lists element by element and collect the And while traversing, we'll be appending every non-matching item to a new (and Using sum() + zip(), we can get sum of one of the list as summation of 1 if both the index in two lists have equal elements, and then compare that number with size of other list. This also requires first to check if two lists are equal before this computation. It also checks for the order.
How to Compare Two Lists in Python, It applies the passed function to each item of the iterable and then returns a map object i.e. an iterator as the result. The functools.reduce() method applies the Comparing the next item in a list . to txt file/drop each index into new line ; python 3, mix 2 lists in print based on a list in Python? Flatten a two
Streamlined for-loop for comparing two lists, Your lists contain 19 elements - so your solution performs an order of 361 operations. for i in range(len(list1)): # assuming the lists are of the same length if If so, Python's equal operator can operate on three arguments, making it Which zips up the list and returns the indices of all the pairs which match. One of the methods is using the Python Set. It first converts the lists into sets and then gets the unique part out of that. Other non-set methods compare two lists element by element and collect the unique ones. We can implement these by using nested for loops and with the list comprehension.
Determining if all Elements in a List are the Same in Python, If these values are equal, then the list consists of the same elements. used a useful Python feature - the ability to compare lists with just the comparison Then the elements from these two lists are compared in turn, and as a If numbers, perform numeric coercion if necessary and compare. If either element is a number, then the other element is "larger" (numbers are "smallest"). Otherwise, types are sorted alphabetically by name. If we reached the end of one of the lists, the longer list is "larger."
- This doesn't work as I expect
- Thanks to everyone for the the excellent answers, I chose this as the best because of it's conciseness and use of examples
itertools.productis nice, too.
- @bereal didn't know about that one, nice suggestion.
- Out of curiosity, what does
mapreturn 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
lst1with each element of
lst2, so should be 9.
- @JesseMu Sorry read the question wrongly, have fixed it to use
- @SuperBiasedMan zip returns an iterator in Python 3 , not a list.
- Thanks for pointing out the correct terminology, this is a product operation
- 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).