Python Program To Remove Duplicates From List
“How to remove duplicates from list” is a popular question and very frequently asked in interviews also. There are so different methods to remove duplicate values for list. Here we are going to cover the general methods. First method is using set properties, and another that using for loop in a simple way. The last one is using dictionary.
Table Of Contents
01. Method: Using Set Properties.
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]
02. Method: Using For Loops.
01. Example
Apply on string list.
def removeDuplicate(duplicate): data = [] for num in duplicate: if num not in data: data.append(num) return data cars = ['Lamborghini','Honda', 'Jeep', 'Honda', 'BMW','Hyundai', 'Hyundai', 'BMW', 'Ford'] print("Array: n", cars) result = removeDuplicate(cars) print("nnArray After Removing Duplicate:n",result)
Output:
Array: ['Lamborghini', 'Honda', 'Jeep', 'Honda', 'BMW', 'Hyundai', 'Hyundai', 'BMW', 'Ford'] Array After Removing Duplicate: ['Lamborghini', 'Honda', 'Jeep', 'BMW', 'Hyundai', 'Ford']
02. Example
Apply on numeric list.
def removeDuplicate(duplicate): data = [] for num in duplicate: if num not in data: data.append(num) return data numbers = [23, 45, 34, 78, 23, 78, 12, 89, 45, 34, 54, 34] print("Array: n", numbers) result = removeDuplicate(numbers) print("nnArray After Removing Duplicate:n",result)
Output:
Array: [23, 45, 34, 78, 23, 78, 12, 89, 45, 34, 54, 34] Array After Removing Duplicate: [23, 45, 34, 78, 12, 89, 54]
03. Method: Using Dictionary.
01. Example
Apply on string list.
cars = ['Lamborghini','Honda', 'Jeep', 'Honda', 'BMW','Hyundai', 'Hyundai', 'BMW', 'Ford'] print("Array: n", cars) result = list(dict.fromkeys(cars)) print("nnArray After Removing Duplicate:n",result)
Output:
Array: ['Lamborghini', 'Honda', 'Jeep', 'Honda', 'BMW', 'Hyundai', 'Hyundai', 'BMW', 'Ford'] Array After Removing Duplicate: ['Lamborghini', 'Honda', 'Jeep', 'BMW', 'Hyundai', 'Ford']
02. Example
Apply on numeric list.
numbers = [23, 45, 34, 78, 23, 78, 12, 89, 45, 34, 54, 34] print("Array: n", numbers) result = list(dict.fromkeys(numbers)) print("nnArray After Removing Duplicate:n",result)
Output:
Array: [23, 45, 34, 78, 23, 78, 12, 89, 45, 34, 54, 34] Array After Removing Duplicate: [23, 45, 34, 78, 12, 89, 54]