Python Program To Remove Duplicates From A List Using Set

Python Program To Remove Duplicates From A List Using Set

 

01. Example

Apply on string list.

cars = ['Lamborghini','Honda', 'Jeep', 'Honda', 'BMW','Hyundai', 'Hyundai', 'BMW', 'Ford']
print("Array: n", cars)
print("nnArray After Removing Duplicate:n",list(set(cars)))

Output:

Array: 
 ['Lamborghini', 'Honda', 'Jeep', 'Honda', 'BMW', 'Hyundai', 'Hyundai', 'BMW', 'Ford']


Array After Removing Duplicate:
 ['Jeep', 'Ford', 'Honda', 'Lamborghini', 'Hyundai', 'BMW']

 

02. Example

Apply on numeric list.

numbers = [23, 45, 34, 78, 23, 78, 12, 89, 45, 34, 54, 34]

print("Array: n", numbers)
print("nnArray After Removing Duplicate:n",list(set(numbers)))

Output:

Array: 
 [23, 45, 34, 78, 23, 78, 12, 89, 45, 34, 54, 34]


Array After Removing Duplicate:
 [34, 12, 45, 78, 54, 23, 89]

 

Leave a comment