How to compare each element of two lists in python

11,497

Solution 1

List comprehension:

[item in b for item in a]

This creates a new list in a way similar to the following code:

newList = []
for item in a:
    newList.append(item in b)

where item in b evaluates to True if item exists in b, otherwise it will evaluate to False.


As mentioned in comments (thanks Paul Rooney!), the speed of this can be improved if you make b into a set:

b_set = set(b)
result = [item in b_set for item in a]

This is because the lookup operation item in b takes consistent time if b is a set, while every single item has to be compared until a matching one is found if b is a list.

The speed improvement is not very noticeable if b is small, but for a list b containing hundreds of elements, this can be a freat improvement.

Solution 2

Because python isn't a primarily numerical or scientific language it doesn't come with some things that are available by default in matlab or R. That being said, almost anything you'd need from those languages is available in the numpy/scipy ecosystem. For example, numpy has an in1d function:

import numpy
a = ['word1','word2','word3','word4']
b = ['word2','word4']

print(numpy.in1d(a, b))
# [False  True False  True]
Share:
11,497
Tezcatlipoca
Author by

Tezcatlipoca

Updated on June 14, 2022

Comments

  • Tezcatlipoca
    Tezcatlipoca almost 2 years

    I am looking for a Python operator similar to %in% in R. It compares all elements in a list to all elements to another list and returns a boolean array. It would do the following:

    a=['word1','word2','word3','word4']
    b=['word2','word4']
    a *%in%* b
    >>False True False True
    

    The closest thing I have found is pd.str.contains but it is not vectorized, i.e. it only looks for one element. Hopefully somebody knows.

  • Sheldore
    Sheldore over 5 years
    Add some explanation. The OP looks new to python
  • iBug
    iBug over 5 years
    @Bazingaa Sure.
  • Paul Rooney
    Paul Rooney over 5 years
    You could make the time complexity linear by making b a set.
  • alkasm
    alkasm over 5 years
    Related (since OP mentioned Pandas and is from R), pd.Series(['word1', 'word2', 'word3', 'word4']).isin(['word2', 'word4']). Didn't think this was worth a new answer but would be a good addition to yours.