Python - How to remove duplicate elements from List - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorials, we will see how to remove duplicate elements from a python list.
We can accomplish this using different ways, let’s see all the ways.
The list of elements is reading from user input.
Removing duplicates using membership operators in python.
elem = eval(input("Enter List of values : "))
list1=[]
for x in elem:
    if x not in list1:
        list1.append(x)
print("After Removing duplicate elements ",list1)
Output:
Enter List of values : [10,20,30,40,10,20,50,60]
After Removing duplicate elements  [10, 20, 30, 40, 50, 60]
Removing duplicate elements using Python’s list comprehension.
list1=[10,20,30,10,50,20,10,60,80,50,40,10]

newlist=[ele for n,ele in enumerate(list1) if ele not in list1[:n]]

print("New List : ",newlist)
Output:
New List :  [10, 20, 30, 50, 60, 80, 40]
Removing duplicates usind sorted() function.
list1 = ['a','c','a','d','e','a','f','b']  
list2 = sorted(set(list1),key=list1.index)  
print(list2)
Output:
['a', 'c', 'd', 'e', 'f', 'b']
If you are concerning about insertion order, you can also remove duplicate elements using python set data structure.
elem = eval(input("Enter List of values : "))
set1 = set(elem)
print("After Removing duplicate elements ",set1)
Output:
Enter List of values : [1,2,3,4,5,1,2,6,4]
After Removing duplicate elements  {1, 2, 3, 4, 5, 6}
Let’s consider, our list containing string elements. We can remove the duplicates from below set functions.
list1 = ["abc", "xyz", "pqr"]
list2 = ["aei", "oup", "xyz","abc"]

newSet= set(list1).symmetric_difference(list2).union(set(list1).intersection(list2))

print(newSet)
Output:
You can find more explanation about the output, from our earlier example of the set data structure.
{'pqr', 'aei', 'abc', 'xyz', 'oup'}
 
Reference:
Python List Data Structure
Python Set Data Structure
Python operators
Happy Learning 🙂

Python Set Data Structure in Depth
Java 8 How to get common elements from two lists
Python Tuple Data Structure in Depth
Python How to read input from keyboard
How to Create or Delete Directories in Python ?
Python – How to remove key from dictionary ?
How to Remove Spaces from String in Python
JQuery Remove Elements Example
Java 8 how to remove duplicates from list
How to Rotate Elements in List
What are the List of Python Keywords
Python List Data Structure In Depth
How to Convert Python List Of Objects to CSV File
How to Find Duplicate Words Count in a File
How to merge two lists in Python

Python Set Data Structure in Depth
Java 8 How to get common elements from two lists
Python Tuple Data Structure in Depth
Python How to read input from keyboard
How to Create or Delete Directories in Python ?
Python – How to remove key from dictionary ?
How to Remove Spaces from String in Python
JQuery Remove Elements Example
Java 8 how to remove duplicates from list
How to Rotate Elements in List
What are the List of Python Keywords
Python List Data Structure In Depth
How to Convert Python List Of Objects to CSV File
How to Find Duplicate Words Count in a File
How to merge two lists in Python
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list