Answer to Question #329091 in Python for ratul

Question #329091

Write a function called rem_duplicate that takes a tuple in the parameter and return a tuple

removing all the duplicate values. Then print the returned tuple in the function call.

[Cannot use remove() or removed() for this task]

===================================================================

Hints:

Unlike lists, tuples are immutable, so the tuple taken as an argument cannot be modified. But

the list can be modified and lastly for returning the result use type conversion. You need to use

membership operators (in, not in) for preventing adding any duplicates values.

===================================================================

Example1:

Function Call:

rem_duplicate((1,1,1,2,3,4,5,6,6,6,6,4,0,0,0))

Output:

(1, 2, 3, 4, 5, 6, 0)

===================================================================

Example2:

Function Call:

rem_duplicate(("Hi", 1, 2, 3, 3, "Hi",'a', 'a', [1,2]))

Output:

('Hi', 1, 2, 3, 'a', [1, 2])


1
Expert's answer
2022-04-20T03:44:57-0400
def rem_duplicate(tup):
	res = []
	for el in tup:
		if el not in res:
			res.append(el)
	return tuple(res)


print(rem_duplicate((1,1,1,2,3,4,5,6,6,6,6,4,0,0,0)))
print(rem_duplicate(("Hi", 1, 2, 3, 3, "Hi",'a', 'a', [1,2])))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS