I'd like to know how to find the common items on the two lists

Asked 2 years ago, Updated 2 years ago, 125 views

I want to know how to find only common items on the two lists.

It would be nice if the -like_item_finding() returns [5]

a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]
Find the same_item_ (a, b)

list python

2022-09-22 22:12

1 Answers

The most Python-like method is

a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]
result = list(set(a) & set(b))
a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]

result = [x for x in a if x in b]


2022-09-22 22:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.