some_iterable=["I","have","no","idea","how","many","values","are","here","so","this","will","be","fun!"]first,second,*_,last=*some_iterableprint(first,second,last)# I have fun!
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...
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.
Software developer, Technical writer, and long-life learner. I enjoy creating software from scratch and sharing knowledge with stunning technical articles. Visit my website -> https://danidiaztech.com
Software developer, Technical writer, and long-life learner. I enjoy creating software from scratch and sharing knowledge with stunning technical articles. Visit my website -> https://danidiaztech.com
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.
Software developer, Technical writer, and long-life learner. I enjoy creating software from scratch and sharing knowledge with stunning technical articles. Visit my website -> https://danidiaztech.com
Software developer, Technical writer, and long-life learner. I enjoy creating software from scratch and sharing knowledge with stunning technical articles. Visit my website -> https://danidiaztech.com
Worked 10 years ago in vb dot net and SQL server. After a gap of 10 years starting a fresh. Good understanding of logic and a very good problem solving approach
Software developer, Technical writer, and long-life learner. I enjoy creating software from scratch and sharing knowledge with stunning technical articles. Visit my website -> https://danidiaztech.com
Software developer, Technical writer, and long-life learner. I enjoy creating software from scratch and sharing knowledge with stunning technical articles. Visit my website -> https://danidiaztech.com
Unpacking gets even better than that...
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...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.
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.
If you want, you can add directly here the link 😉
Heh, I will once I have one. It'll be this fall, tho.
Sweet, man! I didn't even know these existed!!
The * python iterator is kind of like the ES6
[...arr]in a way.It seems Python and Js are not that different 🤣
Hey! I think I know you ...
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.
You can use
some_list.reverse()
also
Of course. That is a useful method,
Nice! Some neat tips in here.
Thanks 🙏🏼
Great knowledge shared.. Keep up the good work
fantastic article! i learnt many things new
Thanks for the feedback 🙏
Amazing article keep it up
I'm glad you find it useful!