To achieve this I have defined a function name : check_duplicate_tuple()
In this function I have passed a tuple as a function parameter.
Inside this function I have declared one list variable duplicate=[] which is used to store duplicate tuples that we will print if there is a duplicate item.
A for loop is used to iterate through each element of tuple and checked for count. If any of the element count is greater than one then that element will get appended to a list we declared.
Function: Below is the code snippet of function which will check if tuple value is duplicate or not and return the duplicate values
def check_duplicate_tuple(l):
#declared a list variable in which we will store duplicate values
duplicate=[]
#using this for loop we will try to check count for tuple element
# and append to list if it occurs more than one
for i in l:
if l.count(i) > 1:
#print("It has duplicate values")
duplicate.append(i)
else:
continue
return duplicate
Full Code:
def check_duplicate_tuple(l):
#declared a list variable in which we will store duplicate values
duplicate=[]
#using this for loop we will try to check count for tuple element
# and append to list if it occurs more than one
for i in l:
if l.count(i) > 1:
#print("It has duplicate values")
duplicate.append(i)
else:
continue
return duplicate
def main():
tup1 = (2,3,5,4,4,2,6,6, 11.56)
#calling function to check duplicate tuple
duplicate=check_duplicate_tuple(tup1)
print("duplicate tuple values are "+ str(list(set(duplicate))))
if __name__ == '__main__':
main()
Hope the above information and code will be helpful.
No comments:
Post a Comment