DEV Community

Discussion on: 15 Python tips and tricks every beginner should know !

Collapse
 
cjsmocjsmo profile image
Charlie J Smotherman

A neat trick with sets

alist = [1, 2, 3, 3, 3, 4, 4, 5]
list(set(alist))
Enter fullscreen mode Exit fullscreen mode

will give you

[1,2,3,4,5]
Enter fullscreen mode Exit fullscreen mode

There is also the reverse() method which reverses the list in-place, while [::-1] gives a new list in reversed order.

casper = [1,2,3,4]
casper.reverse()
Enter fullscreen mode Exit fullscreen mode

gives you

[5,4,3,2,1]
Enter fullscreen mode Exit fullscreen mode

Happy Coding

Collapse
 
aidri profile image
AIDRI

Yes! For the first trick, it is a very cool property of the set function to get unique values.
For the second trick, I explained in my post that this built-in function is much slower, but it is probably easier to remember, I admit :)

Some comments have been hidden by the post's author - find out more