DEV Community

Cover image for 10 Awesome Pythonic One-Liners Explained

10 Awesome Pythonic One-Liners Explained

Andreas on July 29, 2020

Since I wrote my first lines of code in Python, I was fascinated by its simplicity, excellent readability and its popular one-liners in particular....
Collapse
 
waylonwalker profile image
Waylon Walker

Great article, I really like the idea of the easily consumable content. I may think of how I can do something like this in my own way 😉


I am surprised that you made it through 10 one-liners with no ternary statement.

>>> 'Hi ' if 'Bob' in ['Alice', 'Bob', 'Pete'] else 'Goodbye'
'Hi'
Collapse
 
devmount profile image
Andreas

My pleasure 😊

I didn't cover everything on purpose, but I totally forgot about the ternary statement 😅 So thank you for this addition! 👍🏻

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
devmount profile image
Andreas

Good point, thank you for this clarification!

PS: For the sake of correctness: it's readlines() without the underscore, isn't it?

Collapse
 
bulletmark profile image
Mark Blakeney

Sorry, I was actually incorrect. I thought readlines() was a generator but apparently it isn't so I deleted my comment.

Collapse
 
dimensionjohns profile image
norway subway

That is great one. I want to translate it into Urdu and Hindi for my users at techlarapoint.com/bombitup-apk/. Can I please?

Collapse
 
devmount profile image
Andreas

Of course, that would be great! Just provide links to the original article, my Twitter and Github.

Collapse
 
pylixm profile image
DeanWu

Very good article, can I translate it into Chinese and publish it on my WeChat official account? I will specify the source and author. Hope to get your authorization.

Collapse
 
devmount profile image
Andreas

Hey there, I'm glad you like my article! You are welcome to translate it into Chinese. Please set links to the article, my dev.to profile and my Twitter profile at the beginning and let me know, when you published it ✔️

Collapse
 
pylixm profile image
DeanWu

Thank you for your authorization. This is the link to the translated article. Thanks again!
mp.weixin.qq.com/s/_K40z0t_z-sSNDC...

Thread Thread
 
devmount profile image
Andreas

Wow that was fast! 👏🏻 Thanks for the translation and credits. Can you create actual links or are you limited to only show urls as text?

Thread Thread
 
pylixm profile image
DeanWu

The WeChat link is restricted. This should be a temporary link, which can only be accessed from WeChat. The link will become invalid after a period of time. I put the article on my blog so that everyone can check it at any time. Thank you again for your authorization.
pylixm.cc/posts/2020-08-05-10-One-...

Thread Thread
 
devmount profile image
Andreas

I see, thank you! 😎

Collapse
 
paddy3118 profile image
Paddy3118
  1. Read file into array of lines

Needs to use rstrip rather than strip if you want to preserve left-hand whitespace in lines.

Collapse
 
devmount profile image
Andreas

Thank you for this addition. In this example I just wanted to strip both ends. To only remove the linebreaks, you have to specify the linebreak character:

c = [line.rstrip('\n') for line in open('file.txt')]
Collapse
 
anharrington profile image
Andrew N. Harrington

If you know the last line of the file does have a '\n', then line[:-1] is shorter.

Thread Thread
 
devmount profile image
Andreas

Indeed, thank you!

Thread Thread
 
paddy3118 profile image
Paddy3118

That's a big if. rstrip('\n') or simple rstrip() i find easier to read and less error prone.

Collapse
 
devmount profile image
Andreas

Thanks a lot for encouraging me 🤗

Collapse
 
mikaelho profile image
mikaelho

This one I sort of learned today:

' '.join([v for v in [first, middle, last] if v])

Concatenate space-separated full name out of parts, where any of the parts may be an empty string or None.

Collapse
 
paddy3118 profile image
Paddy3118
  1. List mapping

Best to use list comprehensions instead of map in most cases. For your example:

l = [int(x) for x in ['1', '2', '3']]
Collapse
 
devmount profile image
Andreas

I agree, this increases readability even more! Thank you. Is there any other benefit in using list comprehensions instead of the (specially made for this purpose) map() function?

Collapse
 
paddy3118 profile image
Paddy3118

map returns a map object that you then turn into a list. Readability. There are other comprehensions that follow naturally from list comprehensions to generate, for example, dicts, sets and iterators.

Collapse
 
veganaise profile image
Antoine • Edited

I didn't know most of these tricks. Good article and great clarity 😁

Collapse
 
devmount profile image
Andreas

Awesome, thank you! I'm glad it was helpful 😊

Collapse
 
jingxue profile image
Jing Xue

Nice tips. Although wouldn't you want to use with when reading a file as well? I know it doesn't really matter for short scripts, but it's still a good habit to get into.

Collapse
 
zodman profile image
Andres 🐍 in 🇨🇦

My favs:

python -m json.tool

python3 -m http.server

xD

Collapse
 
devmount profile image
Andreas

Nice, thanks for this addition!
Can you add some explanation for those who don't know, what these one-liners do?

Collapse
 
bartszy profile image
bartszy

Great little article, list/set comprehensions are really awesome !

Collapse
 
devmount profile image
Andreas

Indeed they are! Thanks a lot 😊

Collapse
 
ravikeshare profile image
Ravi

Nice Article. 10th one is enough to explain you why people love Python so much, for doing similar thing in other languages would require you to write a numerous lines of code :)

Collapse
 
devmount profile image
Andreas

I'm glad you like it 😊 and it's true: I love Python, because its concepts are so well thought through 👌🏻