Ask Question
24 September, 17:35

Write a function get_common_elems (lst1, lst2) that takes two lists of integers, lst1, and lst2, and returns a new list containing the elements in common between the two lists. You can assume that there are no repeated elements in the lists. Also, the ordering of the elements in the returned new list is not significant. The function signature is given below: def get_common_elems (lst1, lst2) : sig : list (int), list (int) - > list (int) For example, a call to get_common_elems ([5,1,4,3,6],[6,1,8,3]) will return the list 1, 3, 6. Hint: Think about how you can make use of the in operator. You only need to iterate over one of the lists.

+5
Answers (1)
  1. 24 September, 19:51
    0
    def get_common_elems (lst1, lst2):

    common_list = []

    for e in lst1:

    if e in lst2:

    common_list. append (e)

    return common_list

    Explanation:

    Create a function called get_common_elems that takes two lists, lst1, and lst2

    Inside the function:

    Create an empty list to hold the common elements

    Initialize a for loop that iterates through the lst1.

    If an element in lst1 is also in the lst2, append that element to the common_list

    When the loop is done, return the common_list
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a function get_common_elems (lst1, lst2) that takes two lists of integers, lst1, and lst2, and returns a new list containing the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers