Rules to Python one-liners | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Rules to Python one-liners

I have been trying to get into one-liners. I am opposed to them from a readability stand point, but they are cool and fun and make me feel like something else ;) But, I get stuck more often than not. So, my question: Are there rules to one-liners? Is there specific one-liner syntax, in such that there are rules in Python (perhaps non standard rules) that are catered towards one-liners? Also, tips and tricks are welcome

20th Sep 2019, 9:26 AM
Brave Tea
Brave Tea - avatar
2 Answers
0
Look up list comprehensions, generator expressions, lambda functions, ternary syntax and built-in functions. Those r the techniques used most often for writing one-liners. You should also get yourself acquainted with the star operator and double star operator (for dics). That's about it I think. But honestly, it is usually not worth it.
20th Sep 2019, 1:04 PM
Thoq!
Thoq! - avatar
0
i very rarly use them. Mostly I will use them if I am trying to generate something on the fly in a code “like a Lambda situation” but nothing else. I agree with the guys. Hard to read, maintain and I hate seeing them in a source code that is not mine, means hours of tail chasing.
20th Sep 2019, 4:50 PM
Mohamed El-Sayed
Mohamed El-Sayed - avatar