जब दी गई टपल्स की लिस्ट से उन टपल्स को हटाने की आवश्यकता होती है जिनमें डुप्लीकेट पहली वैल्यू होती है, तो एक सिंपल 'for' लूप, और 'add' और 'append' मेथड्स का उपयोग किया जा सकता है।
इसके लिए एक प्रदर्शन नीचे दिया गया है:
उदाहरण
my_input = [(45.324, 'Hi Jane, how are you'), (34252.85832, 'Hope you are good'), (45.324, 'You are the best.')]
visited_data = set()
my_output_list = []
for a, b in my_input:
if a not in visited_data:
visited_data.add(a)
my_output_list.append((a, b))
print("The list of tuple is : ")
print(my_input)
print("The list of tuple after removing duplicates is :")
print(my_output_list)आउटपुट
The list of tuple is : [(45.324, 'Hi Jane, how are you'), (34252.85832, 'Hope you are good'), (45.324, 'You are the best.')] The list of tuple after removing duplicates is : [(45.324, 'Hi Jane, how are you'), (34252.85832, 'Hope you are good')]
व्याख्या
- टपल्स की एक लिस्ट परिभाषित की जाती है, और कंसोल पर प्रदर्शित की जाती है।
- एक खाली सेट बनाया जाता है, साथ ही एक खाली लिस्ट।
- टपल्स की लिस्ट पर इटरेट किया जाता है, और यदि यह 'set' में मौजूद नहीं है, तो इसे सेट में जोड़ा जाता है, साथ ही लिस्ट में भी।
- यह आउटपुट है जो कंसोल पर प्रदर्शित होता है।