I have two lists list1
and list2
. I've found on stackoverflow a very simple method to get the common elements in this two lists as follows result = list(set(list1) & set(list2))
. Unfortunately, with this, the order of elements in the resulting list, is not preserved.
For instance:
list1 = ['a', 'e', 't', 'b', 'c']
list2 = ['e', 'b', 'a', 'c', 'n', 's']
I want the result (common elements) to be ['e', 'a', 'b', 'c']
in this order. Because, for instance, 'e' is in list1 and in list2 and is in position 2 in list1 and position 1 in list2, while 'a' is in list1 and in list2 and is in position 1 in list1 and position 3 in list2, so 'e' is before 'a' since 2+1 < 1+3.
So, is there any simple way to have the common elements between two lists and preserving the order of elements ?
Method 1:Using Set's & property Convert the lists to sets and then print set1&set2. set1&set2 returns the common elements set, where set1 is the list1 and set2 is the list2. Below is the Python3 implementation of the above approach: Python3.
Yes, the order of elements in a python list is persistent.
Find the common items from two lists using set(). intersection() Example 3- Using set( ). intersection(), We can print the list of common elements of a list, but it can not store in any of the variables.
Here we will see how to get common elements from two lists we can even say the intersection of two lists using different ways. retainAll () method from the Collection interface is used to remove the common elements from two different lists. We even get the common elements from two lists using java 8 stream API distinct () method.
In this article, we will understand how to find common elements present in two given lists if they exist, in Python using Python sets. We will be doing so by creating Python set objects using the given lists and then comparing them using the & operator and using the set intersection () method.
Given two lists, the task is to write a Python program to remove all the common elements of two lists. Explanation: Lists after removing common elements of both the lists i.e, 4 and 5. between them. The remove () method removes the first matching element (which is passed as an argument) from the list.
Original lists: ['red', 'green', 'black', 'orange'] ['red', 'pink', 'green', 'white', 'black'] ['white', 'orange', 'pink', 'black'] Test common elements between color1 and color2 are in same order? True Test common elements between color1 and color3 are in same order? False Test common elements between color2 and color3 are in same order?
You could use a list comprehension to filter all elements from list1
that don't also belong to list2
:
list1 = ['a', 'e', 't', 'b', 'c']
list2 = ['e', 'b', 'a', 'c', 'n', 's']
result = [item for item in list1 if item in list2]
print result
Result:
['a', 'e', 'b', 'c']
Although this does not conform to the desired result in your main post, from your follow-up comment it seems like this is an acceptable result.
You could also continue using the set approach, and then sort the results after the fact using the positioning algorithm you described:
list1 = ['a', 'e', 't', 'b', 'c']
list2 = ['e', 'b', 'a', 'c', 'n', 's']
items = set(list1) & set(list2)
result = sorted(items, key=lambda element: list1.index(element) + list2.index(element))
print result
Result:
['e', 'a', 'b', 'c']
list1 = ['a', 'e', 't', 'b', 'c']
list2 = ['e', 'b', 'a', 'c', 'n', 's']
weights = defaultdict(int)
for i, e in enumerate(list1):
weights[e] += i
for i, e in enumerate(list2):
weights[e] += i
>>> result = sorted(set(list1) & set(list2), key=lambda i: weights[i])
>>> result
['e', 'a', 'b', 'c']
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