Shrink List for Repeating Elements - Python Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a list of elements that may contain repeated values. Our task is to shrink the list by ensuring that each repeated element appears only once while maintaining the order of their first occurrence. For example, if the input list is [2, 3, 2, 5, 3, 5], the output should be [2, 3, 5].Using set This method uses a set to track elements that have already been added to the output. It is efficient because sets provide fast membership checks. Python a = [2, 3, 2, 5, 3, 5] s = set() res = [] for x in a: if x not in s: s.add(x) res.append(x) print(res) Output[2, 3, 5] Explanation:A set is used to keep track of elements that have already been added to the result list.For each element in the input list, we check if it is in the set. If not, we add it to both the set and the result list.The order of elements is preserved as we only append elements to the result list when encountering them for the first time.Let's explore some more methods and see how we can shrink given list for repeating elements.Table of ContentUsing dictionary from collectionsUsing list comprehension with setUsing for loop and in operatorUsing dictionary from collectionsThis method uses the OrderedDict class from the collections module to maintain the order of unique elements. Python from collections import OrderedDict a = [2, 3, 2, 5, 3, 5] res = list(OrderedDict.fromkeys(a)) print(res) Output[2, 3, 5] Explanation:The OrderedDict.fromkeys function is used to create an ordered dictionary where each key is a unique element from the input list.The keys of the ordered dictionary are then converted back to a list, preserving the order.This method is concise and avoids the need for a loop and an explicit set.Using list comprehension with setThis method combines list comprehension and a set to achieve the same result but in a single line of code. Python a = [2, 3, 2, 5, 3, 5] s = set() res = [x for x in a if x not in s and not s.add(x)] print(res) Output[2, 3, 5] Explanation:A set is used to track seen elements during list comprehension.The not s.add(element) ensures that each element is added to the set only once while constructing the result list.Using for loop and in operatorThis method checks for the presence of each element in the result list using the in operator and for loop before adding it. Python a = [2, 3, 2, 5, 3, 5] res = [] for x in a: if x not in res: res.append(x) print(res) Output[2, 3, 5] Explanation:'in' operator checks if an element is already in the result list.If not, the element is appended to the result list.This method is less efficient compared to the others due to the repeated membership checks on the list, which are slower than set operations. Comment More infoAdvertise with us Next Article Python - Optional padding in list elements S Smitha Dinesh Semwal Follow Improve Article Tags : Python Python Programs python-list Python list-programs Practice Tags : pythonpython-list Similar Reads Repeat Each Element K times in List - Python The task of repeating each element k times in a list in Python involves creating a new list where each element from the original list appears k times consecutively. For example, given the list a = [4, 5, 6] and k = 3, the goal is to produce [4, 4, 4, 5, 5, 5, 6, 6, 6]. Using list comprehensionList c 3 min read Python - Repeat Alternate Elements in list Many times we have this particular use-case in which we need to repeat alternate element of list K times. The problems of making a double clone has been discussed but this problem extends to allow a flexible variable to define the number of times the element has to be repeated. Letâs discuss certain 7 min read Python - Optional padding in list elements Optional padding in list elements involves adding extra elements, such as zeros or placeholders, to a list to ensure it reaches a desired length. This technique is useful when working with fixed-size data structures or when aligning data in lists.Using List ComprehensionThis method pads each element 2 min read Python | Retain K Front and Rear elements Sometimes, we require to shrink a list by deletion of its certain elements. One of the methods that is employed to perform this particular task is front and rear element retention and deletion of rest elements. It is a good utility whose solution can be useful to have. Letâs discuss certain ways in 8 min read String Repetition and spacing in List - Python We are given a list of strings and our task is to modify it by repeating or adding spaces between elements based on specific conditions. For example, given the list `a = ['hello', 'world', 'python']`, if we repeat each string twice, the output will be `['hellohello', 'worldworld', 'pythonpython']. U 2 min read Python - Maximum N repeated Elements Given a List of elements, we need to remove an element if it's occurrence in the list increases more than N. For example,Input : a = [6, 4, 6, 3, 6], N = 1Output : [6, 4, 3]Explanation : The occurrence 2nd onwards of 6 are removed.Input : a = [6, 4, 6, 3, 6], N = 2Output : [6, 4, 6, 3]Explanation : 4 min read Like