DEV Community

Cover image for 6 Python Tips & Tricks that no One Teaches 🚀🐍

6 Python Tips & Tricks that no One Teaches 🚀🐍

Daniel Diaz on April 26, 2021

Python is currently the most used programming language in the world, and the unique reason is that Python developers are happy building software wi...
Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

Unpacking gets even better than that...

some_iterable = ["I", "have", "no", "idea", "how", "many", "values", "are", "here", "so", "this", "will", "be", "fun!"]
first, second, *_, last = *some_iterable
print(first, second, last)  # I have fun!
Enter fullscreen mode Exit fullscreen mode

You can include one starred name among the names to unpack into. Python will fill in all the others based on position, and then stuff the rest into the starred name.

In this case, I use _ for the name, because I really only intend to throw those away. _ is a completely valid name in Python, but it's conventionally used as a "throw away" name. If you're curious, you can still see what was parked in there in the above code with...

print(_)  # ['no', 'idea', 'how', 'many', 'values', 'are', 'here', 'so', 'this', 'will', 'be']
Enter fullscreen mode Exit fullscreen mode

P.S. Bit of shameless self-promotion, all of the above is in my forthcoming book "Dead Simple Python" (No Starch Press, 2021). So, it isn't quite that "no one" covers this stuff.

Collapse
 
danidiaztech profile image
Daniel Diaz

What such an amazing explanation Jason.🤯
That's exactly why I said the unpack operator deserves an isolated article.

BTW, I'd be glad to read your book.

Collapse
 
devlorenzo profile image
DevLorenzo

If you want, you can add directly here the link 😉

Collapse
 
codemouse92 profile image
Jason C. McDonald

Heh, I will once I have one. It'll be this fall, tho.

Collapse
 
ironcladdev profile image
Conner Ow

Sweet, man! I didn't even know these existed!!
The * python iterator is kind of like the ES6 [...arr] in a way.

Collapse
 
devlorenzo profile image
DevLorenzo

Hey! I think I know you ...

Collapse
 
danidiaztech profile image
Daniel Diaz

It seems Python and Js are not that different 🤣

Collapse
 
crestiancik profile image
Crestiancik • Edited

Wow, that is a really great post though! I am very surprised by those tricks and tips actually, as I did not even expect that you can do this kind of things. And, that is true, no one actually teaches you those things, which makes them even more amazing! I am learning python right now actually, and I have to tell you that most of the teachers tend to avoid from teaching us different useful tips and tricks, the only place where you could actually find these tricks are the python learning websites, nurtem.com/classes-and-camps/progr... is one of them, and probably the best one, at least the best ones.

Collapse
 
arunj0shi profile image
Arun Joshi

fantastic article! i learnt many things new

Collapse
 
danidiaztech profile image
Daniel Diaz

Thanks for the feedback 🙏

Collapse
 
coreyjs profile image
Corey Schaf

Nice! Some neat tips in here.

Collapse
 
danidiaztech profile image
Daniel Diaz

Thanks 🙏🏼

Collapse
 
deepcjoshi10 profile image
Deep

Great knowledge shared.. Keep up the good work

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
danidiaztech profile image
Daniel Diaz

I'm glad you find it useful!

Collapse
 
guptaarth87 profile image
guptaarth87

Amazing article keep it up

Collapse
 
cjsmocjsmo profile image
Charlie J Smotherman

You can use

some_list.reverse()

also

Collapse
 
danidiaztech profile image
Daniel Diaz

Of course. That is a useful method,