0 votes
1 view
in Python by (47.8k points)

I want to take two lists and find the values that appear in both.

a = [1, 2, 3, 4, 5]

b = [9, 8, 7, 6, 5]

returnMatches(a, b)

would return [5], for instance.

1 Answer

0 votes
by (107k points)

There are many ways to compare two lists in python and return their matches some of the important methods are as follows:-

a = [1, 2, 3, 4, 5]

b = [9, 8, 7, 6, 5]

set(a) & set(b)

image

Another method you can use is the list comprehensions if an order is significant, below is the code that shows how we use list-comprehension:-

a = [1, 2, 3, 4, 5]

b = [9, 8, 7, 6, 5]

[i for i, j in zip(a, b) if i == j] 

image

Related questions

0 votes
1 answer
asked Jul 5, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
asked Jul 2, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
asked Jul 13 in Python by ashely (43.2k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...