Package: screwduplicates
provides a simple and efficient way to remove duplicates from an iterable (even with unhashable elements, optional order preservation)pip install screwduplicatesTested against Windows 10 / Python 3.10 / AnacondaDuplicate Removal:It effectively removes duplicate elements from an iterable, ensuring that the returned iterable only contains unique values. This can be useful in scenarios where duplicate values need to be eliminated to avoid redundant or incorrect data.Flexibility:The function accepts a wide range of iterable types, including lists, tuples, and custom iterable objects. It can handle both hashable and unhashable elements, accommodating diverse data types.Order Preservation:The function provides an option to preserve the original order of elements in the iterable by setting the keep_order parameter to True. This is valuable when the order of elements matters, such as maintaining the sequence of items in a list.Error Handling:If the input iterable contains unhashable elements, the function gracefully handles the TypeError that arises during the conversion to a set. Instead of raising an error, it automatically switches to preserving order and includes a representation of the unhashable element in the output.testdict={"List":[5,4,1,2,2,3,4,5],"Set":{1,2,3,4,5},"Tuple":(1,3,4,2,3,4,5,5,5,2,2,1),"Dictionary":{"a":1,"b":2,"c":3},"String":"Hello","Bytes":b"Hello","Bytearray":bytearray(b"Hello"),"List of strings":["Hello","World","Hello"],"List of numbers":[2.5,3.5,1.5,2.5,3.5],"Empty list":[],"Set of strings":{"apple","banana","cherry"},"Set of numbers":{1,2,3,4,5},"Tuple of strings":("apple","banana","cherry","banana","banana"),"Tuple of numbers":(3,4,5,1,2,3,4,2,3,4,2,3,4,5),"Dictionary with numbers":{1:"one",2:"two",3:"three"},"Dictionary with strings":{"a":"apple","b":"banana","c":"cherry"},"List of mixed types":[1,"a",2.5,True,True,True,1,"a","Hello"],"Set of mixed types":{1,"a",2.5,True,"Hello"},"Tuple of mixed types":(1,"a",2.5,True,"Hello",1,"a",2.5),"Nested list":[[1,2],[3,4],[5,6],[1,2],[3,4],[3,4],[5,6]],"Nested set":{frozenset({1,2}),frozenset({3,4}),frozenset({5,6})},"Nested tuple":((1,2),(3,4),(5,6),(3,4)),"List with None value":[1,None,3,None,5],"Set with None value":{1,None,3,None,5},"Tuple with None value":(1,None,3,None,5),"List with boolean values":[True,False,True],"Set with boolean values":{True,False},"Tuple with boolean values":(True,False,True),"List with repeated values":[1,1,1,1,1],"Set with repeated values":{1,1,1,1,1},"Tuple with repeated values":(1,1,1,1,1),"List with mixed types and duplicates":[1,"a",2,"a",3,True,3],"Set with mixed types and duplicates":{1,"a",2,"a",3,True},"Tuple with mixed types and duplicates":(1,"a",2,"a",3,True),"List with empty strings":["","World","World","Hello","World"],"Set with empty strings":{"","World","Hello","World","World"},}fromscrewduplicatesimportdel_duplicatesforkey,itemintestdict.items():print(f"\n---------------\n{key}:")print(f"Original:{item}")print(f"dont't keep order:{del_duplicates(item,keep_order=False)}")print(f"Keep order:{del_duplicates(item,keep_order=True)}")---------------List:Original:[5,4,1,2,2,3,4,5]dont't keep order: [1, 2, 3, 4, 5]Keeporder:[5,4,1,2,3]---------------Set:Original:{1,2,3,4,5}dont't keep order: {1, 2, 3, 4, 5}Keeporder:{1,2,3,4,5}---------------Tuple:Original:(1,3,4,2,3,4,5,5,5,2,2,1)dont't keep order: (1, 2, 3, 4, 5)Keeporder:(1,3,4,2,5)---------------Dictionary:Original:{'a':1,'b':2,'c':3}dont't keep order: {'a': 1, 'b': 2, 'c': 3}Keeporder:{'a':1,'b':2,'c':3}---------------String:Original:Hellodont't keep order: loeHKeeporder:Helo---------------Bytes:Original:b'Hello'dont't keep order: b'Hleo'Keeporder:b'Helo'---------------Bytearray:Original:bytearray(b'Hello')dont't keep order: bytearray(b'Hleo')Keeporder:bytearray(b'Helo')---------------Listofstrings:Original:['Hello','World','Hello']dont't keep order: ['World', 'Hello']Keeporder:['Hello','World']---------------Listofnumbers:Original:[2.5,3.5,1.5,2.5,3.5]dont't keep order: [1.5, 2.5, 3.5]Keeporder:[2.5,3.5,1.5]---------------Emptylist:Original:[]dont't keep order: []Keeporder:[]---------------Setofstrings:Original:{'cherry','banana','apple'}dont't keep order: {'cherry', 'banana', 'apple'}Keeporder:{'cherry','banana','apple'}---------------Setofnumbers:Original:{1,2,3,4,5}dont't keep order: {1, 2, 3, 4, 5}Keeporder:{1,2,3,4,5}---------------Tupleofstrings:Original:('apple','banana','cherry','banana','banana')dont't keep order: ('cherry', 'banana', 'apple')Keeporder:('apple','banana','cherry')---------------Tupleofnumbers:Original:(3,4,5,1,2,3,4,2,3,4,2,3,4,5)dont't keep order: (1, 2, 3, 4, 5)Keeporder:(3,4,5,1,2)---------------Dictionarywithnumbers:Original:{1:'one',2:'two',3:'three'}dont't keep order: {1: 'one', 2: 'two', 3: 'three'}Keeporder:{1:'one',2:'two',3:'three'}---------------Dictionarywithstrings:Original:{'a':'apple','b':'banana','c':'cherry'}dont't keep order: {'a': 'apple', 'b': 'banana', 'c': 'cherry'}Keeporder:{'a':'apple','b':'banana','c':'cherry'}---------------Listofmixedtypes:Original:[1,'a',2.5,True,True,True,1,'a','Hello']dont't keep order: [1, 2.5, 'a', 'Hello']Keeporder:[1,'a',2.5,'Hello']---------------Setofmixedtypes:Original:{1,2.5,'a','Hello'}dont't keep order: {1, 2.5, 'a', 'Hello'}Keeporder:{1,2.5,'a','Hello'}---------------Tupleofmixedtypes:Original:(1,'a',2.5,True,'Hello',1,'a',2.5)dont't keep order: (1, 2.5, 'a', 'Hello')Keeporder:(1,'a',2.5,'Hello')---------------Nestedlist:Original:[[1,2],[3,4],[5,6],[1,2],[3,4],[3,4],[5,6]]dont't keep order: [[1, 2], [3, 4], [5, 6]]Keeporder:[[1,2],[3,4],[5,6]]---------------Nestedset:Original:{frozenset({3,4}),frozenset({5,6}),frozenset({1,2})}dont't keep order: {frozenset({3, 4}), frozenset({5, 6}), frozenset({1, 2})}Keeporder:{frozenset({3,4}),frozenset({5,6}),frozenset({1,2})}---------------Nestedtuple:Original:((1,2),(3,4),(5,6),(3,4))dont't keep order: ((1, 2), (3, 4), (5, 6))Keeporder:((1,2),(3,4),(5,6))---------------ListwithNonevalue:Original:[1,None,3,None,5]dont't keep order: [1, 3, None, 5]Keeporder:[1,None,3,5]---------------SetwithNonevalue:Original:{1,3,None,5}dont't keep order: {1, 3, None, 5}Keeporder:{1,3,None,5}---------------TuplewithNonevalue:Original:(1,None,3,None,5)dont't keep order: (1, 3, None, 5)Keeporder:(1,None,3,5)---------------Listwithbooleanvalues:Original:[True,False,True]dont't keep order: [False, True]Keeporder:[True,False]---------------Setwithbooleanvalues:Original:{False,True}dont't keep order: {False, True}Keeporder:{False,True}---------------Tuplewithbooleanvalues:Original:(True,False,True)dont't keep order: (False, True)Keeporder:(True,False)---------------Listwithrepeatedvalues:Original:[1,1,1,1,1]dont't keep order: [1]Keeporder:[1]---------------Setwithrepeatedvalues:Original:{1}dont't keep order:{1}Keeporder:{1}---------------Tuplewithrepeatedvalues:Original:(1,1,1,1,1)dont't keep order: (1,)Keeporder:(1,)---------------Listwithmixedtypesandduplicates:Original:[1,'a',2,'a',3,True,3]dont't keep order: [3, 1, 2, 'a']Keeporder:[True,'a',2,3]---------------Setwithmixedtypesandduplicates:Original:{'a',1,2,3}dont't keep order: {'a', 1, 2, 3}Keeporder:{'a',1,2,3}---------------Tuplewithmixedtypesandduplicates:Original:(1,'a',2,'a',3,True)dont't keep order: (3, 1, 2, 'a')Keeporder:(True,'a',2,3)---------------Listwithemptystrings:Original:['','World','World','Hello','World']dont't keep order: ['', 'World', 'Hello']Keeporder:['','World','Hello']---------------Setwithemptystrings:Original:{'','World','Hello'}dont't keep order: {'', 'World', 'Hello'}Keeporder:{'','World','Hello'}