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....
For further actions, you may consider blocking this person and/or reporting abuse
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.
My pleasure 😊
I didn't cover everything on purpose, but I totally forgot about the ternary statement 😅 So thank you for this addition! 👍🏻
Good point, thank you for this clarification!
PS: For the sake of correctness: it's
readlines()
without the underscore, isn't it?Sorry, I was actually incorrect. I thought readlines() was a generator but apparently it isn't so I deleted my comment.
That is great one. I want to translate it into Urdu and Hindi for my users at techlarapoint.com/bombitup-apk/. Can I please?
Of course, that would be great! Just provide links to the original article, my Twitter and Github.
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.
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 ✔️
Thank you for your authorization. This is the link to the translated article. Thanks again!
mp.weixin.qq.com/s/_K40z0t_z-sSNDC...
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?
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-...
I see, thank you! 😎
Needs to use rstrip rather than strip if you want to preserve left-hand whitespace in lines.
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:
If you know the last line of the file does have a '\n', then line[:-1] is shorter.
Indeed, thank you!
That's a big if.
rstrip('\n')
or simplerstrip()
i find easier to read and less error prone.Thanks a lot for encouraging me 🤗
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.
Best to use list comprehensions instead of map in most cases. For your example:
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?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.
I didn't know most of these tricks. Good article and great clarity 😁
Awesome, thank you! I'm glad it was helpful 😊
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.My favs:
python -m json.tool
python3 -m http.server
xD
Nice, thanks for this addition!
Can you add some explanation for those who don't know, what these one-liners do?
Great little article, list/set comprehensions are really awesome !
Indeed they are! Thanks a lot 😊
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 :)
I'm glad you like it 😊 and it's true: I love Python, because its concepts are so well thought through 👌🏻