Question

PYTHON 3 HELP Write a function find_common_elements(list_p,list_q) that accepts two lists, list_p and list_q, as parameters...

PYTHON 3 HELP

Write a function find_common_elements(list_p,list_q) that accepts two lists, list_p and list_q, as parameters and returns a new list that contains only the elements that are common between two lists (without duplicates). Make sure your program works on two lists of different sizes. Demonstrate your function works by calling it with 2 different pairs of lists.

Pseudocode for find_common_elements(list_p,list_q):

INITIALIZE empty common_list
FOR EACH element e in list_p:
    IF e is in list_q:
        ADD e to common_list
RETURN common_list
0 0
Add a comment Improve this question Transcribed image text
Answer #1
def find_common_elements(list_p, list_q):
    common_list = []
    for num in list_p:
        if num in list_q and num not in common_list:
            common_list.append(num)
    return common_list


print(find_common_elements([1, 2, 3], [2, 3, 4, 5]))
print(find_common_elements([2, 3], [1, 5, 7]))

Add a comment
Know the answer?
Add Answer to:
PYTHON 3 HELP Write a function find_common_elements(list_p,list_q) that accepts two lists, list_p and list_q, as parameters...
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
  • 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...

  • Given 2 lists, a and b, containing integers, not necessarily with the same length, Write a...

    Given 2 lists, a and b, containing integers, not necessarily with the same length, Write a Python program(Python 3) that returns a list that contains only the elements that are common between the lists a and b (without duplicates).

  • Write a method called alternate that accepts two Lists as its parameters and returns a new...

    Write a method called alternate that accepts two Lists as its parameters and returns a new List containing alternating elements from the two lists, in the following order: • First element from first list • First element from second list • Second element from first list • Second element from second list • Third element from first list • Third element from second list If the lists do not contain the same number of elements, the remaining elements from the...

  • In Python: Write the definition for a function named avg which accepts two int parameters, and...

    In Python: Write the definition for a function named avg which accepts two int parameters, and returns the average of the two values Write a statement that calls this function to print out the average of values 11 and 25.

  • Write a complete program utilizing functions. The function accepts two parameters: one representing a radius of...

    Write a complete program utilizing functions. The function accepts two parameters: one representing a radius of type float, the other parameter is an integer “select” which chooses between finding the area of a circle or the volume of sphere. If the select variable is 1, then area is calculated. If the select value is 2, then volume is calculated. If the select value is any other value, then the function returns 0. Test your function in main by calling it...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • PYTHON: Using Lists and Arrays! 5. In a program, write a function that accepts two arguments:...

    PYTHON: Using Lists and Arrays! 5. In a program, write a function that accepts two arguments: a list, and a number n. The program should generate 20 random numbers, in the range of 1 through 100, and assign each number to the list. The program should also ask the user to pick a number from 1 through a 100 and assign that number to n. The function should display all of the number in the list that are greater than...

  • python In a program, write a function that accepts two arguments: a list, and a number...

    python In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display the number n, the list of numbers, and a sub list of all the numbers in the list that are greater than the number n. Initialize the list and the number n in the main function of your code and call your function, passing the list and number as arguments. Run your code...

  • PYTHON QUESTION PLEASE!! Write a function named problem3 that accepts two strings as the arguments, returns...

    PYTHON QUESTION PLEASE!! Write a function named problem3 that accepts two strings as the arguments, returns the characters that occur in both strings. Test case: the arguments are “apple@123” and “banana@#345”, your program should return “a@3”. Write a function named problem3 that accepts two strings as the arguments, returns the characters that occur in both strings. (20 pts) Test case: the arguments are "apple@123” and “banana@#345”, your program should return "a@3".

  • Function Name: zip_d Parameters: two equal-sized lists Returns: a dictionary Description: Write a function, zip_d, that...

    Function Name: zip_d Parameters: two equal-sized lists Returns: a dictionary Description: Write a function, zip_d, that takes a two equal-sized lists as parameters, and zips each list into a dictionary. Sample Inputs/Outputs: Example Call: zip_d([1,2,3], ['a','b','c']) Expected Return: {1: 'a', 2: 'b', 3: 'c'} Example Call: zip_d([‘a’,’b’,’c’],[9.8,7.6,5.4]) Expected Return: {‘a’:9.8, ’b’:7.6, ’c’:5.4} please answer in python

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