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
Β 
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
Β 
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
Β 
devmount profile image
Andreas β€’

Thanks a lot for encouraging me πŸ€—

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
Β 
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
Β 
veganaise profile image
Gabrielle β€’ β€’ 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
Β 
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 πŸ‘ŒπŸ»

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
Β 
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
Β 
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?