Question

Using the count function, create a function that returns only the unique elements in a list....

Using the count function, create a function that returns only the unique elements in a list. Now try to do the same thing using the set function instead of count. For example: f([1,2,2,3]) gives back [1,2,3].

Homework Answers

Answer #1

Explanation:

Here is the solution with both the methods, using the count function and also with the set function.

Code:

Using count function:


def f(l):
  
res = []
  
for each in l:
if(l.count(each) >= 1 and each not in res):
res.append(each)
  
return res


print(f([1, 2, 2, 3]))

using set function:


def f(l):
  
  
return list(set(l))
  

print(f([1, 2, 2, 3]))

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!

Know the answer?
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
Similar Questions
Create a function that returns a boolean value if all-elements are not odd or even. Using...
Create a function that returns a boolean value if all-elements are not odd or even. Using Dr. Racket and the How to Design a Function (HtDF ) recipe, create a function that will return the following check-expect: (check-expect (all-elements? even? (list 1 2 3) false) (check-expect (all-elements? even? (list 2 4 6) true) (check-expect (all-elements? odd? (list 1 3 5) true) Can you find an element in the list where the predicate fails (return false)?
** Language Used : Python ** PART 2 : Create a list of unique words This...
** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...
using dr.racket programing language If we write a function that tests whether a list contains only...
using dr.racket programing language If we write a function that tests whether a list contains only strings, odd numbers, or even numbers, you will notice that the code that iterates through the list stays the same, with the only change being the predicate function that checks for the desired list element. If we were to write a new function for each of the tests listed above, it would be more error-prone and an example of bad abstraction. We could write...
(USING ML) (Using ML) Write a function dupList of type 'a list -> 'a list whose...
(USING ML) (Using ML) Write a function dupList of type 'a list -> 'a list whose output list is the same as the input list but with each element of the input list repeated twice in a row. For example, if the input is [1, 2, 3], the output list should produce [1, 1, 2, 2, 3, 3]. If the input list [], the output list should be []. Do not use explicit recursion but use one of the fold...
Create a list of at least 3 unique goods or services whose quality has improved over...
Create a list of at least 3 unique goods or services whose quality has improved over time in such a way that current prices do not accurately reflect their real prices, even adjusted for inflation. Now try to come up with at least 1 item where the quality has decreased over time. Which was easier to come up with? What does this say about inflation and deflation? P.S. Please include definitions words such as nominal prices, inflation, deflation, nominal income,...
Create a function called, convert. This function receives a string parameter called word which only contains...
Create a function called, convert. This function receives a string parameter called word which only contains digits (the string represents a positive number) and returns a list of numbers. This is how the function works: - This function calculates the number of times each digit has repeated in the input string and then generates a number based on that using the following formula and adds it to a list. For instance, if the digit x has been repeated n times,...
In python Please Problem We want to create a DHCP service that assigns a random, unique...
In python Please Problem We want to create a DHCP service that assigns a random, unique IP with subnet mask 255.0.0.0. This means the service only sets the last 3 octets of an IP. For example if the dhcp_service function is called by an IP of "192.0.0.0", it should keep the 192 and change the rest: 1. Create a dhcp_service(ip_address) function, outside of the ServerClass 2. Have request_dhcp_ip call the dhcp_service and set the ServerClass object's IP to the generated...
PLEASE DO IT IN C++ ONLY. THE MIN-MAX DIGIT PROBLEM Write a function named minMaxDigit() that...
PLEASE DO IT IN C++ ONLY. THE MIN-MAX DIGIT PROBLEM Write a function named minMaxDigit() that accepts an integer as an input parameter and returns the largest and smallest digits using the two output parameters min and max. For example, the call minMaxDigit(68437, min, max) would set min to 3 and max to 8. If there is only one digit, then both min and max are set to the same value. The function has no return statement.
Using C programming Create a function called printMenu( ) with the following properties: Has no function...
Using C programming Create a function called printMenu( ) with the following properties: Has no function inputs or output. Prints the following menu to the screen: 1. Enter user name. 2. Enter scores. 3. Display average score. 4. Display summary. 5. Quit Create a function called printLine( ) with the following properties: Takes as input a char Takes as input an integer corresponding to the number of times to print the character Has no function output. For example, if we...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT