
Thanks for creating this awesome resource, JAM, and RJW!īecause I learned a lot from studying the one-liners, I thought why not revive the page (after almost ten years since the last change happened)?Īfter putting a lot of effort into searching the web for inspiration, I created the following ten one-liners. I visited this page oftentimes and I loved studying the one-liners presented above. Overview: 10 one-liners that fit into a tweet Github '''Python One-Liners''' - Share your own one-liners with the community Python One-Line X - How to accomplish different tasks in a single line Interesting Quora Thread ''Python One-Liner'' So, use your one-liner superpower wisely!įree Python One-Liners Learning Resourcesįree ''Python One-Liners'' videos & book resourcesĬollection of ''One-Liners'' with interactive shell However, if you use well-established one-liner tricks such as list comprehension or the ternary operator, they tend to be Pythonic. As a rule of thumb: if you use one-liners that are confusing, difficult to understand, or to show off your skills, they tend to be Unpythonic. Of course, there is debate on whether one-liners are even Pythonic. The source code is contributed from different Python coders - Thanks to all of them! Special thanks to the early contributor JAM.


Please use a "sorted insert" for your new one-liner.) ( Edit: The one-liners are now sorted more or less by ease-of-understanding - from simple to hard.

It would be awesome if this page expanded to the point where it needs some sort of organization system. The trick is to think of something that will "do a lot with a little." Most importantly, reading and writing about Python one-liners (e.g., in this post) is a lot of fun! There's even a whole subculture around who can write the shortest code for a given problem. In other languages (think: Java) this would be nearly impossible, but in Python, it's a lot easier to do. You may ask: why should I care? The answer is profound: if you cannot read and write one-liner code snippets, how can you ever hope to read and write more complicated codebases? Python one-liners can be just as powerful as a long and tedious program written in another language designed to do the same thing. This page is devoted to short programs that can perform powerful operations called Python One-Liners.
