duplicate python list code example

Example 1: python remove duplicates from list

# remove duplicate from given_list using list comprehension
res = []
[res.append(x) for x in given_list if x not in res]

Example 2: remove duplicates python

mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))

Example 3: duplicate in list python

a = [1,2,3,2,1,5,6,5,5,5]

import collections
print([item for item, count in collections.Counter(a).items() if count > 1])

## [1, 2, 5]

Example 4: python duplicate list elements

def duplicate_list_elements(_list : list):
    duplicated_list = []

    for element in _list:
        duplicated_list.append(element)
        duplicated_list.append(element)

    return duplicated_list