

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to print duplicates from a list of integers?
Here we are trying to print all the duplicate numbers from a list of numbers. So, we are trying to print all the numbers which occur more than once in a list (not unique in the list).
Examples
Input: given_list = [ 3, 6, 9, 12, 3, 30, 15, 9, 45, 36, 12] Output: desired_output = [3, 9, 12] Input: given_list = [-27, 4, 29, -27, -2 , -99, 123, 499, -99] Output: desired_output = [-27, -99]
Below is the code to find the duplicates elements from a given list −
lst = [ 3, 6, 9, 12, 3, 30, 15, 9, 45, 36, 12, 12] dupItems = [] uniqItems = {} for x in lst: if x not in uniqItems: uniqItems[x] = 1 else: if uniqItems[x] == 1: dupItems.append(x) uniqItems[x] += 1 print(dupItems)
Output
[3, 9, 12]
Above program will work not only for a list of integers but others too −
Input: given_list = ['abc','def','raj','zack','abc','raj'] Output: output_returned= ['abc', 'raj']
- Related Questions & Answers
- C# program to print duplicates from a list of integers
- Java program to print duplicates from a list of integers
- Python program to remove Duplicates elements from a List?
- Python Program to print unique values from a list
- Java program to remove duplicates elements from a List
- Python - Ways to remove duplicates from list
- Python program to print all sublists of a list.
- Python Program to Print Nth Node from the last of a Linked List
- Java Program to Remove Duplicates from an Array List
- Python Program to print element with maximum vowels from a List
- Remove duplicates from a List in C#
- C# program to print unique values from a list
- Java program to print unique values from a list
- Java Program to merge duplicates of a List with TreeSet
- C++ Program to Remove Duplicates from a Sorted Linked List Using Recursion
Advertisements