Common Elements Finder

Tags: Lists, Set

Write a Python function to find the common elements between two lists. The function should return a list that contains the common elements.

Example 1:

Input: [1,2,3,4,5], [4,5,6,7,8] 
Output: [4,5]

Example 2:

Input: ['apple', 'banana', 'cherry'], ['cherry', 'date', 'elderberry'] 
Output: ['cherry']

You can use Python’s set operations to find the common elements between two lists.

def find_common(list1, list2):
    return list(set(list1) & set(list2))

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!