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].
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!
Get Answers For Free
Most questions answered within 1 hours.