Ask Question
10 October, 01:54

Given the lists, lst1 and lst2, create a new sorted list consisting of all the elements of lst1 that also appears in lst2. For example, if lst1 is [4, 3, 2, 6, 2] and lst2 is [1, 2, 4], then the new list would be [2, 2, 4]. Note that duplicate elements in lst1 that appear in lst2 are also duplicated in the new list. Associate the new list with the variable new_list, and don't forget to sort the new list.

+2
Answers (1)
  1. 10 October, 02:19
    0
    lst1 = [4, 3, 2, 6, 2]

    lst2 = [1, 2, 4]

    new_lst = []

    for i in lst1:

    if i in lst2:

    new_lst. append (i)

    new_lst. sort ()

    print (new_lst)

    Step-by-step explanation:

    The code is written in python.

    lst1 = [4, 3, 2, 6, 2]

    The variable lst1 represent a list of integers.

    lst2 = [1, 2, 4]

    The variable lst2 represent a list of integers.

    new_lst = []

    An empty variable is created new_lst and it is used to store the values of lst1 that is in lst2.

    for i in lst1:

    The code loop through integers in lst1.

    if i in lst2:

    This code means if any of the value in lst1 is in lst2.

    new_lst. append (i)

    This code put the same values found in lst1 and lst2 in a new list (new_lst)

    new_lst. sort ()

    We sort the value of the new list from the smallest to the biggest.

    print (new_lst)

    The new list is displayed
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Given the lists, lst1 and lst2, create a new sorted list consisting of all the elements of lst1 that also appears in lst2. For example, if ...” in 📗 Mathematics if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers