Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (47.6k 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 (106k 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

Interested to learn more about Python? Come & join our Python online course

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.2k users

Browse Categories

...