Remove Duplicate Elements From A List In Python (2 Ways)

Remove Duplicate Elements From A List In Python (2 Ways)

Patrick Loeber

2 года назад

156,294 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

gaming land
gaming land - 29.05.2023 02:49

it's only works if all elements same data type i mean 25 and "25" will treated differently , i ran into similar issues i had to convert all elements to same data type and then do list set

Ответить
Achim
Achim - 30.01.2023 00:21

np.unique

Ответить
manoj m
manoj m - 08.11.2022 10:29

I love your tips so much .
They are so much useful. Keep doing it thank you

Ответить
Baka Senpai
Baka Senpai - 24.10.2022 22:17

Use numpy unique instead

Ответить
Sheikh Akbar
Sheikh Akbar - 15.10.2022 12:23

Python secrets and power tips are why I come from time to time to this wonderful channel.

Ответить
1ur_br0th3r
1ur_br0th3r - 14.10.2022 14:38

There is another method: for x in list: for y in cleared_list: if x == y: break; cleared_list.append(x)

Ответить
MANISH__ KUMAR
MANISH__ KUMAR - 11.10.2022 11:20

Sir please tell how to make pandas series in python in vs code

Ответить
Random GBL SFS
Random GBL SFS - 10.10.2022 20:46

But why the set changes the order?

Ответить
Munis Khaled
Munis Khaled - 24.08.2022 17:20

oh thank you, this way actually saved my time

Ответить
Metasy
Metasy - 19.08.2022 09:22

np.unique

Ответить
nobody everybody
nobody everybody - 14.08.2022 04:22

you are genius, wirklich ))

Ответить
Mohammed Albayati
Mohammed Albayati - 09.08.2022 18:59

And i was doing "for" loops lol 😂

Ответить
Just AnotherSoul
Just AnotherSoul - 24.05.2022 17:43

sorted(set([3,2,1,1,1,1,3]))
-> [1,2,3]

Ответить
Coding Optimized
Coding Optimized - 06.05.2022 21:40

While searching for my own video I got your video 😂

Ответить
Jasjeet Singh
Jasjeet Singh - 27.04.2022 07:35

In ruby on rails we just do arr.uniq

Ответить
M CC
M CC - 25.04.2022 02:37

Short and Sweet!

Ответить
shadwen 22
shadwen 22 - 24.04.2022 02:53

you can also do it this way:
define a new list, and set it to be empty

and using the in operator check if the number is in the new list or not
if not then it will be added to the new list otherwise it will be skipped

e.g:

numbers = [1,2,3,3,4,5,5]
uniques = []
for number in numbers:
if number not in uniques:
uniques.append(number)
print(uniques)


also if you use *insert*(0,number) instead of append it will be ordered backwards

Ответить
hashbrowntag
hashbrowntag - 20.04.2022 08:54

you could just sort the array and then iterate through the array and check if the adjacent element is the same as the previous element

Ответить
Azhar Syed
Azhar Syed - 20.04.2022 07:03

i use ordered dict for making sure I have the same order.

Ответить
Pufferfish2584
Pufferfish2584 - 19.04.2022 16:41

OrderedSet? Or A = []; [A.append(b) for b in B if b not in A]

Ответить
Peter Tailor
Peter Tailor - 18.04.2022 20:58

Love your videos!!! Best time I ever spent watching yt short :D

Ответить
LasevIX_
LasevIX_ - 08.04.2022 23:11

You can also use the built in method of
`x.union(x)`, it automatically removes all duplicates

Ответить
Thygrrr
Thygrrr - 06.04.2022 20:06

Why is the dict guaranteed to be ordered if it's not an OrderedDict?

Ответить
Robin Koch
Robin Koch - 04.04.2022 19:30

Even if order is guaranteed in this case, I would not rely on implicitly ordered dictionaries.

Ответить
JJ Ferman
JJ Ferman - 02.04.2022 05:13

Pretty sure python versions is important here. Previous versions of python, dictionaries were not ordered

Ответить
Mr. Marcial Glori
Mr. Marcial Glori - 01.04.2022 08:29

I wanna do it in java.

Ответить
CesarD321
CesarD321 - 29.03.2022 17:21

In JS just set(sort())

Ответить
Stevine Tynol
Stevine Tynol - 29.03.2022 09:11

Or you create an empty list to copy the values of the original list only once
ori_list = [7,4,8,12,7,1,4]
emp_list = [ ]
for I in ori_list():
if I not in emp_list()
emp_list.append(i)
print(emp_list)
Even though yours isn't as long. And you used a simgle function

Ответить
TheCetarius
TheCetarius - 21.03.2022 20:19

Pandas unique()

Ответить
Saumy Tiwari
Saumy Tiwari - 21.03.2022 20:15

Why not use list comprehension

Ответить
ML Guy
ML Guy - 14.03.2022 01:36

Hands up the people who did two pointer technique?

Ответить
Eat Breathe Data Science
Eat Breathe Data Science - 23.02.2022 17:43

May I know which software you use to do your video editing ? Thank you !

Ответить
Yousef Imran
Yousef Imran - 18.02.2022 22:34

This is pretty much open source life (Languages, OS, etc.)
Person 1: "What do we call something that removes duplicates from a list?"
Person 2: "Maybe, have an object type called List that supports RemoveDups method as in My list.RmoveDups()?"
Person 1: "Nonsense! Have you not heard open source rule #1? All of our naming conventions must give no god damn clue as to what they do!"

Ответить
TheFootballPlaya
TheFootballPlaya - 16.02.2022 04:15

do this one next: Find the Most Common Number(s) in a List. Note: Number(s), as in, it is possible that multiple elements are the most common.
extra challenge could be: List elements can be anything, not necessarily just numbers, it could be dictionaries / lists / tuples, etc. Still find the most common elements.

Ответить
comedyclub333
comedyclub333 - 14.02.2022 16:11

While this works many times there is actually no guarantee that it will always work since a dictionary does not need to stay ordered.

Ответить
Sloppy Puppy
Sloppy Puppy - 14.02.2022 05:15

And what is your computation time on that 😂😂😂

Ответить
Clumpfy
Clumpfy - 13.02.2022 17:49

Lol he sounds exactly like every Bad guy german in every movie ever :D

Ответить
H R
H R - 13.02.2022 09:05

Superb, i learnt something new. Thanks.

Ответить
Rohit Agarwal
Rohit Agarwal - 12.02.2022 00:55

Good job my BrODAR. 😆

Ответить
Hamza Sarwar
Hamza Sarwar - 11.02.2022 22:49

numbers = [1,3,3,2,25,7,2,6,8,231,67,5,9,0,7,9,0]
unique_numbers = set()
for number in numbers:
unique_numbers.add(number)
print(numbers)
print(list(unique_numbers))

Output:
[1, 3, 3, 2, 25, 7, 2, 6, 8, 231, 67, 5, 9, 0, 7, 9, 0]
[0, 1, 2, 3, 67, 5, 6, 7, 8, 231, 9, 25]

Ответить
Crown
Crown - 09.02.2022 15:55

Can't you just write this?

my_list = [elements here]
new_list = []

for e in my_list:
if e not in new_list:
new_list.append(e)

Ответить
Not important
Not important - 09.02.2022 09:47

Me, who's currently learning Matlab:
Unique()

Ответить
Patrick Moore
Patrick Moore - 08.02.2022 12:39

Hi people, please do not follow this advice. Not only is likely that this behavior is not guaranteed by the python language (and that it could change in newer versions), but the code does not communicate its intention. Ever written in a codebase that makes no sense? It’s from little things like this.

*3rd option: just code it. It will never break and you can actually read the code.

```

duplicate_list = [56, 4, 81, 9, 56, 4]
cleaned_list = list()
seen = set()
for elem in duplicate_list:
if elem in seen:
continue
cleaned_list.append(elem)
seen.add(elem)
```

Note: it might be possible to do this with a list comprehension, but I’m not sure if you can sneak the set.add in there somehow. The loop just works and is easy to follow!

Ответить
Snazzie
Snazzie - 08.02.2022 05:29

Or use lodash

Ответить
Sam Yesuraj
Sam Yesuraj - 07.02.2022 17:41

Hi I was recently doing linear regression algorithm from scarch...

when i use my algorithm to predict it works finely with the datasets that is available in scikit learn...

but when i made a dataset of my own.... the values of bias and weights become very large ..,. and when the bias and weights get added or subracted it get some error like ur values become infinity so u cant add or reduce... is that any problem in my algorithm or is that any problem with my dataset

for i in range(5,200,9):
c = (i - 32) * (5/9)
X_train.append([c])
y_train.append(i)

for i in range(239,320,9):
c = (i - 32) * (5/9)
X_test.append([c])
y_test.append(i)


This is how i made my own dataset is there any problem in this.......

Ответить
Бородатый
Бородатый - 06.02.2022 23:04

What about writing a loop, and use another list or some kind of a data structure that allows to find elements in an efficient way, and... Oh no no no 😲

Ответить
Vibhakar Solanki
Vibhakar Solanki - 06.02.2022 11:32

Only when on >py3.6 otherwise use ordered dict

Ответить
Abdullah
Abdullah - 06.02.2022 09:58

Orduh

Ответить
Sachit Bansal
Sachit Bansal - 05.02.2022 21:03

Can't we simply use for loop

Ответить