Question

Python help: Given the lists, lst1 and lst2, create a new sorted list consisting of all...

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)

  1. SyntaxError: expected an indented block (CTest.py, line 3)
  2. 3 if i in lst2:
0 0
Add a comment Improve this question Transcribed image text
Answer #1

"""

Your logic is correct but there is an indentation

error. So I am providing you screenshot of solution

along with code

"""

#python code

lst1 = [4, 3, 2, 6, 2]
lst2 = [1, 2, 4]
new_list = []
for i in lst1:
    if i in lst2:
        new_list.append(i)
new_list.sort()
print(new_list)

#Output

//If you need any help regarding this solution ...... please leave a comment.. thanks

Add a comment
Know the answer?
Add Answer to:
Python help: Given the lists, lst1 and lst2, create a new sorted list consisting of all...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lists for Python Language

    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. Associatethe new list with the variable new_list , and don't forget to sort the...

  • . Write a Scheme program for each of the following. Which takes in two lists “lst1” and “lst2”, b...

    . Write a Scheme program for each of the following. Which takes in two lists “lst1” and “lst2”, both of which are individually sorted (in ascending order), and returns a list containing the elements of both lst1 and lst2 in sorted order. Print the final list. Input to the program are “lst1” and “lst2”. For instance, lst1 = (1 3 5 7) and lst2 = (2 4 6 8) output: (1 2 3 4 5 6 7 8)

  • PYTHON Define a function reverseNestedList(...) which receives one possibly nested list (lst1) and returns the reverse...

    PYTHON Define a function reverseNestedList(...) which receives one possibly nested list (lst1) and returns the reverse of both the given list and any nested lists inside it. Note that the testing inputs will not include lists that are nested more than twice (a list in a list in a list ++...) [[[...],1], 1] Good luck! As an example, the following code fragment: lst1 = [[1, 2], [3, 4]] print(reverseNestedList(lst1)) should produce the output: [[4, 3], [2, 1]]

  • Discrete Mathematics Unsorted and Sorted Lists For linear search there was no requirement for the list...

    Discrete Mathematics Unsorted and Sorted Lists For linear search there was no requirement for the list to be organized in any manner. The linear search works for lists that are "unsorted." But what if the values in the list are given in ascending order? This would be a sorted list. With a sorted list, is there a more efficient way to find the target? Unsorted Lists (4 pts) Assume there is a sorting algorithm with order of growth O(n), where...

  • ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns...

    ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of...

  • (20 points) ) Write a Python program which merges two sorted singly linked lists and return...

    (20 points) ) Write a Python program which merges two sorted singly linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Example: Input: 1->2- >4, 1->3->4 Output: 1->1->2->3 ->4->4

  • please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection...

    please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 4 6 Initial List Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position: Sorted List...

  • IN PYTHON Develop the program that will allow you to obtain an ordered list of whole...

    IN PYTHON Develop the program that will allow you to obtain an ordered list of whole numbers from least to greatest from two lists of whole numbers. The individual values ​​of the input lists are captured by the user one by one and then both lists will be joined and then sorted in ascending order (from lowest to highest value). Your program should allow the capture of the entire values ​​of the lists without making use of messages (legends) to...

  • (Python)Implement the function deep_list, which takes in a list, and returns a new list which contains...

    (Python)Implement the function deep_list, which takes in a list, and returns a new list which contains only elements of the original list that are also lists. Use a list comprehension. def deep_list(seq): "" "Returns a new list containing elements of the original list that are lists. >>> seq = [49, 8, 2, 1, 102] >>> deep_list(seq) [] >>> seq = [[500], [30, 25, 24], 8, [0]] >>> deep_list(seq) [[500], [30, 25, 24], [0]] >>> seq = ["hello", [12, [25], 24],...

  • The language is python thr language is python I Expert Q&A Done The language is python....

    The language is python thr language is python I Expert Q&A Done The language is python. 10. The following is the algorithm for Merge Sort, one of the best sorting algorithms and one hat is recursive. Wine the merge sort function and draw the stack and heap dagrams for execution on the list |5, 8,9,1,4. 10 Algorithm: Mergesort Ingut an unsorted list b 1. If there is only one iten L. It is sorted, return L 2, Split L Sn...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT