1 SIMPLE Trick To REMOVE Duplicates From ANY List In Python 💫

preview_player
Показать описание
Remove duplicates from any list in #Python with this simple trick. #Code #Shorts
Рекомендации по теме
Комментарии
Автор

That's a really nice tip by the way. You can also use it to check if a list contains only unique values:
if len(set(list1)) == len(list1):
# unique

gfher
Автор

..and if you want to keep the order of the list:
list(dict.fromkeys(<list>))

bx_h
Автор

Won't that mess up the order though?
And order is basically the most important trait of a list

moonmountain_
Автор

This will generally destroy the ordering of the list (because sets are unordered). The simplest way to remove duplicates while preserving the ordering is by using a for loop like this:

list_without_duplicates = [ ]
for x in _list:
if x not in list_without_duplicates:

SandroAerogen
Автор

list(dict.fromkeys(_list)) much better aproach, alternative version sorted(set(_list), key=_list.index) cause set is unsorted so u will not have them in same sort as original, but in some cases u don't perhaps need correct order

Sinke_
Автор

Here what I do In this condition:



As we know that dictionary keys cannot be duplicate..,
So we have converted list items into dictionary keys..
And reconverted back to list

saumytiwari
Автор

actually it is not, there is some feature: >>> set([1, 2, 3, True, False, 0])
{False, 1, 2, 3}

ivan
Автор

Note I would recommend using sorted instead of list to insure the order.

oida
Автор

Wow 😳! That's a list, put inside of a set, inside of a list, to remove duplicates. Sound recursively to me 😁.
Thanks for the tip 🍉😉👍 n

dcknature
Автор

Very bad advice. Sets don’t have order, which is one of the main points of lists. You should use

WillyJL
Автор

why are there so much bad python videos like this. A set does not have any order, I'm pretty sure it is undefined behavior and can very well result with another order for whatever algorithm used depending on the interpreter/compiler

jimmyneutron
Автор

I couldn’t get it to work for the pagame Vector2. I just didn’t remove duplicates lol the game was simple and it didn’t impact performance too much. Why work harder when you can work lazier amirite programmers

shlokbhakta
Автор

You didn't actually remove duplicate number you just created a new list with unique numbers

SupreemeSteevee