Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Find all duplicates in a list and populate them in same order in a new list

$
0
0

I have found the following code on Stack Overflow for creating new list of unique values recursively, but it doesn't work for me no matter how I tweak it for my requirement in the title. What is wrong here?

My requirement is for input [1,2,2,3,3,3,4,4,4,4] the output should be [2,3,4] or for the input [1,2,24,2,1] the output should be [1,2].

The code I am trying is as below:

def find_duplicates(list_of_numbers):    #start writing your code here    dup_list=[]    if len(list_of_numbers) < 1:        dup_list=[]    else:        if list_of_numbers[0] in list_of_numbers[1:]:            print(list_of_numbers[0])            dup_list=[list_of_numbers[0]]+find_duplicates(list_of_numbers[1:])        else:            dup_list=find_duplicates(list_of_numbers[1:])    return dup_listlist_of_numbers=[1,2,2,3,3,3,4,4,4,4]list_of_duplicates=find_duplicates(list_of_numbers)print(list_of_duplicates)

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>