175k views
0 votes
Create a function named find_common() The function should take in two parameters, both of them should be lists, and returns a new list of elements that are common in both. The function body should be no longer than one line of code.

User John Huynh
by
8.4k points

1 Answer

4 votes

Final answer:

To find common elements in two lists, use the set intersection operation and convert the resulting set back into a list.

Step-by-step explanation:

To create a function that finds common elements in two lists, you can use the set intersection operation. Here's the one-line code:

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

In this code, we use the set() function to convert each list into a set, which eliminates duplicate elements. Then, we use the & operator to get the intersection of the two sets, which gives us the common elements. Finally, we convert the resulting set back into a list before returning it.

User Greenify
by
7.6k points