0
4.8kviews
Write a Python program to check whether tuple is having duplicate items are not
1 Answer
0
1.2kviews
  • Tuple is a inbuilt data used to store collection of data in a same variable. The collection of data is enclosed in a round brackets . It can have duplicate values and tuples are immutable.
  • Set is a inbuilt method return the unique elements from the collection of dataset & it is unchangeable .Data is enclosed in a curly brackets for set.

Code:-

t=(12,12,23,54,23,23)
#set method collects unique values 
#convert it into tuple & store it in x
x=tuple(set(t))
#compare x and t
if x!=t:
  print("Tuple has duplicate values")
else:
  print("Tuple has no duplicate values")

Output:-

Tuple has duplicate values

So to find if the tuples have duplicates, convert tuple to set by type casting then compare set and tuple if they appear to same then the tuple has no duplicate values else it has duplicate values.

Please log in to add an answer.