लिस्ट एक महत्वपूर्ण कंटेनर है और दिन-प्रतिदिन प्रोग्रामिंग के साथ-साथ वेब-डेवलपमेंट के लगभग हर कोड में उपयोग किया जाता है, जितना अधिक इसका उपयोग किया जाता है, उतना ही इसमें महारत हासिल करने की आवश्यकता होती है और इसलिए इसके संचालन का ज्ञान आवश्यक है। सूची संचालन से डुप्लिकेट निकालें बड़ी संख्या में एप्लिकेशन हैं और इसलिए, इसका ज्ञान होना अच्छा है।
उदाहरण
# using naive methods # initializing list test_list = [1, 3, 5, 6, 3, 5, 6, 1] print ("The original list is : " + str(test_list)) # using naive method to remove duplicated from list res = [] for i in test_list: if i not in res: res.append(i) # printing list after removal print ("The list after removing duplicates : " + str(res)) # using list comprehension # initializing list test_list = [1, 3, 5, 6, 3, 5, 6, 1] print ("The original list is : " + str(test_list)) # using list comprehension to remove duplicated from list res = [] [res.append(x) for x in test_list if x not in res] # printing list after removal print ("The list after removing duplicates : " + str(res)) # using set() # initializing list test_list = [1, 5, 3, 6, 3, 5, 6, 1] print ("The original list is : " + str(test_list)) # using set() to remove duplicated from list test_list = list(set(test_list)) # printing list after removal print ("The list after removing duplicates : " + str(test_list)) # using list comprehension + enumerate() # initializing list test_list = [1, 5, 3, 6, 3, 5, 6, 1] print ("The original list is : " + str(test_list)) # using list comprehension + enumerate() to remove duplicated from list res = [i for n, i in enumerate(test_list) if i not in test_list[:n]] # printing list after removal print ("The list after removing duplicates : " + str(res)) # using collections.OrderedDict.fromkeys() from collections import OrderedDict # initializing list test_list = [1, 5, 3, 6, 3, 5, 6, 1] print ("The original list is : " + str(test_list)) # using collections.OrderedDict.fromkeys() to remove duplicated from list res = list(OrderedDict.fromkeys(test_list)) # printing list after removal print ("The list after removing duplicates : " + str(res))
आउटपुट
The original list is : [1, 3, 5, 6, 3, 5, 6, 1] The list after removing duplicates : [1, 3, 5, 6] The original list is : [1, 3, 5, 6, 3, 5, 6, 1] The list after removing duplicates : [1, 3, 5, 6] The original list is : [1, 5, 3, 6, 3, 5, 6, 1] The list after removing duplicates : [1, 3, 5, 6] The original list is : [1, 5, 3, 6, 3, 5, 6, 1] The list after removing duplicates : [1, 5, 3, 6] The original list is : [1, 5, 3, 6, 3, 5, 6, 1] The list after removing duplicates : [1, 5, 3, 6]