python ways to remove duplicate

Backtworace
1 min readAug 22, 2020

--

python ways to remove duplicate

This article focuses on one of the operations of getting the unique list from a list that contains a possible duplicated. Remove duplicates from list operation has large number of applications and hence, it’s knowledge is good to have.

Method 1 : Naive method
In naive method, we simply traverse the list and append the first occurrence of the element in new list and ignore all the other occurrences of that particular element.

filter_none

edit

play_arrow

brightness_4

# Python 3 code to demonstrate

# removing duplicated from list

# using naive methods

# initializing list

test_list = [1, 3, 5, 6, 3, 5, 6, 1]

print ("The original list is : " + str(test_list))

# using naive method

# to remove duplicated

# from list

res = []

for i in test_list:

if i not in res:

res.append(i)

# printing list after removal

print ("The list after removing duplicates : " + str(res))

--

--

Backtworace
Backtworace

Written by Backtworace

I am founder of <a href=”https://pythonslearning.com”>pythonslearning</a>

No responses yet