Question: Python help: Given the lists, lst1 and lst2, create a new sorted list consisting of all the elements of lst1 that also appears in lst2.
Python help:
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.
I came up with
new_list = [] for i in lst1 : if i in lst2 : new_list.append(i) new_list.sort()
but getting
Code Analysis: Compiler Error(s)
Compiler Error Messages:
The compiler reported... 2 error(s)
- SyntaxError: expected an indented block (CTest.py, line 3)
- 3 if i in lst2:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
