Question

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 new list.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
lst1 = [4, 3, 2, 6, 2]
lst2 = [1, 2, 4, 1, 5]new_list = []
for num in lst1:
    if num not in lst2:
        new_list.append(num)
for num in lst2:
    if num not in lst1:
        new_list.append(num)
new_list.sort()print(new_list)


Add a comment
Answer #2

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 new list.

Add a comment
Know the answer?
Add Answer to:
Lists for Python Language
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
  • 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...

  • . 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)

  • In PYTHON! Exercise 3: Lists and Functions In this exercise we will explore the use of...

    In PYTHON! Exercise 3: Lists and Functions In this exercise we will explore the use of lists and functions with multiple inputs and multiple outputs. Your task is to implement the separate_and_sort function. This function takes two input parameters: 1. A list containing strings and integers in any order. 2. An optional integer parameter size which controls how many items in the list the function will process. If the length of the list is smaller than the value of size,...

  • 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...

  • 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...

  • Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of...

    Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...

  • Dictionaries

    1)You are given a variable, wwII_battle_winners , that is associated with a dictionary that maps the names of World War II battles to the victors of WWII battles. Writesome code that associates a sorted list of the battle names that appear as keys in this dictionary with the variable battles .2)The inverse of a map is a new map where the values of the original map become the keys of the new map and the keys become the values. For...

  • use python 2a. Union (1 points) Make a union function which takes in 2 lists as...

    use python 2a. Union (1 points) Make a union function which takes in 2 lists as parameters and returns a list. It should return a list that contain all elements that are in either list. Your results list should contain no duplicates. For example, if the two lists are [1,5,6,5, 2] and [3,5, 1,9] then the result list is (1,5,6,2,3,9). Note that the list sizes don't have to be equal. Note that the input lists could have duplicates, but your...

  • Problem 3 Implement a mergeLists method in LinkedListTest class that merges 2 given lists and returns...

    Problem 3 Implement a mergeLists method in LinkedListTest class that merges 2 given lists and returns an output list. While merging make sure: 1. Output list is sorted 2. Remove any duplicate Example: listl: 2 479 list2: 4 5 167 merge (listl, list2) Output: 1 2 45679

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