Given a list of integers with duplicate elements in it. The task to generate another list, which contains only the duplicate elements. In simple words, the new list should contain the elements which appear more than one.
Examples :
Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60] Input : list = [-1, 1, -1, 8] Output : output_list = [-1]
Below is the implementation :
# Python program to print # duplicates from a list # of integers def Repeat(x): _size = len(x) repeated = [] for i in range(_size): k = i + 1 for j in range(k, _size): if x[i] == x[j] and x[i] not in repeated: repeated.append(x[i]) return repeated # Driver Code list1 = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] print (Repeat(list1)) # This code is contributed # by Sandeep_anand |
Output :
[20, 30, -20, 60]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python - Ways to print longest consecutive list without considering duplicates element
- Python | Merging duplicates to list of list
- Python | Program to count duplicates in a list of tuples
- Program to print N minimum elements from list of integers
- Python Remove Duplicates from a List
- Python - Ways to remove duplicates from list
- Python | Remove consecutive duplicates from list
- Python | Remove all duplicates and permutations in nested list
- Python | Combine two lists by maintaining duplicates in first list
- Python | Remove duplicates from nested list
- Python | Sort given list by frequency and remove duplicates
- Python | Consecutive duplicates all elements deletion in list
- Python | Remove duplicates based on Kth element tuple list
- Python - List product excluding duplicates
- Python - Difference of List keeping duplicates
- Python | Convert list of numerical string to list of Integers
- Python groupby method to remove all consecutive duplicates
- Python | Remove all duplicates words from a given sentence
- Python | Difference of two lists including duplicates
- Python | Remove duplicates in Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sandeep_anand

