How to Remove Duplicates from List or Array in Python – GATE, Interview & Placement Practice
851 views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I'll show you a very
0:04
common program which is asked inside
0:06
Python interviews competitive
0:07
programming placements as well how to
0:10
remove the duplicates from a list of
0:13
numbers so if you have a array or a list
0:15
of numbers if you have duplicate numbers
0:18
in this case you can see two is
0:21
repeating four is also repeating so if
0:23
you want to remove all these duplicates
0:24
number from this list and this will be
0:26
the output which will be given so 1 3 1
0:30
2 3 4 5 we will remove all the
0:33
duplicates so this is our Python program
0:36
i will show you step by step so if I
0:38
just execute this program here app 16
0:43
py you will see list without duplicates
0:46
1 2 3 4 5 so it removed all the numbers
0:50
which are repeating only having one
0:53
instance only so all the duplicates are
0:56
removed so now let me just show you how
1:00
this program is
1:02
executed let me paste in this software
1:05
which will show you step by step how
1:08
this program executes so first of all we
1:12
have a list of numbers so we have this
1:15
array of list of numbers here so you can
1:18
see this is the zeroth index 1 2 3 4 5 6
1:23
so we have six numbers and uh now we
1:28
will loop through for detecting the
1:30
duplicates we need to declare another
1:33
array here unique numbers which will be
1:35
empty by default so this is the empty
1:37
array which will hold the output the
1:40
unique numbers so again for this uh
1:43
logic guys we are using another for loop
1:47
if you are a Python developer for loop
1:49
is the most used concept you will use
1:54
for loops a lot so you just need to
1:56
learn how to write for loops so here we
1:58
are looping through each number in the
2:00
list we are simply saying for number in
2:03
the actual array here which is your
2:05
numbers we are looping through each
2:07
number and here we are checking that if
2:09
the number is not already in the unique
2:12
number and for actually detecting the
2:15
duplicates we we need to have another
2:17
variable here which is a boolean
2:20
parameter is duplicate is set to false
2:24
so by default the value of is duplicate
2:26
is false and now the number is one
2:29
because we are at this number position
2:32
so here we are simply checking in this
2:34
if condition that if the number is equal
2:37
to this number is not present so because
2:41
the unique numbers is currently empty
2:44
this number is not present so this will
2:46
be added to the unique number so we
2:49
simply add this using the append method
2:51
so we add this one to the unique
2:55
number so this is added right here so
2:58
this is unique now we come to the second
3:01
iteration right here which is we are
3:04
number becomes two two is also not
3:07
present right here in this so two will
3:10
also get
3:13
added so you can see
3:18
that two will be added here now we come
3:22
to the third index which is again two so
3:24
now this becomes interesting because two
3:26
is already present so we come to this
3:30
for loop and
3:33
uh so two is not equal to 1 we compare
3:37
it with the first number two is not
3:40
equal to one so this will not be
3:43
executed we come to the second number
3:45
which is two so 2 is equal to two here
3:48
because two is already present so this
3:51
will not be executed and we come out of
3:53
this so this will be executed because
3:56
this will evaluate to true so now this
3:58
variable will become from false to true
4:01
so it's a duplicate number we'll remove
4:03
this we will ignore this we will simply
4:06
break out of this loop and we come to
4:09
the next
4:13
number so now we come to the next number
4:16
which is three three again is not a
4:19
duplicate it's not present so again it
4:22
will get added then we have four four is
4:25
also not duplicate so it will get
4:28
added then we have again four so four is
4:31
a duplicate number
4:35
so so you compare it 1 2 3 and then the
4:41
last index which is four is equal to
4:44
four so it's a duplicate number so this
4:47
will not be added so then lastly we come
4:50
to the fifth number fifth is a not a
4:53
duplicate it's a unique so it will get
5:00
added so that's all after that it will
5:03
print out this list without duplicates 1
5:06
2 3 4 5 so hopefully you have understood
5:08
this program very simple program we run
5:11
a simple for loop and then we have the
5:14
out inner for loop for checking whether
5:17
the number is unique or not if it is
5:20
unique then we add this if it is a
5:21
duplicate then we break out of the loop
5:24
and ignore it so in this way you can
5:27
write this program and uh also check out
5:30
my website guys free media tools.com uh
5:34
which contains thousands of tools
#Programming
#Scripting Languages