I introduced myself in the programming world using very basic languages. Since 4 years ago, I created many many projects using Python, but although the Python Zen says "There should be one-- and preferably only one --obvious way to do it.", some things aren't obvious.
One line conditionals.
My old code has too redundant code, specially with conditionals:
if x < 56:
some = "Dafaq"
else:
some = "Nope"
Nothing simpler than putting:
some = "Dafaq" if x < 56 else "Nope"
One line filtering lists.
One of the most common situations in Python is "filtering" lists. My old way:
a = [2, 24, 88, 32, 1, 6, 88, 10, 15, 34] # example list.
b = []
for i in a:
if i >= 15:
b.append(i)
Best way:
a = [2, 24, 88, 32, 1, 6, 88, 10, 15, 34] # example list.
b = [i for i in a if i >= 15]
One line sum list values.
Other of the most common things is to sum up the values of a list. My old way:
amounts = [2, 24, 88, 32, 1, 6, 88, 10, 15, 34] # example list.
total = 0
for i in amounts:
total += i
Best way:
amounts = [2, 24, 88, 32, 1, 6, 88, 10, 15, 34]
total = sum(amounts)
In the next chapter...
Things that weren't so obvious when you starting to using Django...
Top comments (27)
Nice post!
Thanks! I don't think I knew about
sum
.For those that want to learn more about "one line filtering lists", they are an example of list comprehensions. In addition to filtering, you can use them to get a list of some property on objects, or perform some operation:
etc.
You can also make dictionary comprehensions.
A related concept is range:
Note that in Python 3, range is not a list but rather its own type. However, you can use it very similarly to a list.
To add to this, the output of the range function is a generator which generates values on the fly as opposed to having them stored in memory all at once.
Furthermore, if you replace the square brackets of a list comprehension with curved brackets, you get a generator comprehension.
Though I initially found Python to be fairly straightforward due to its readability, it definitely took me a while to wrap my head around some of the mutli-operational list comprehensions and a good deal longer than that until I was able to write them with any degree of confidence.
That "one-line conditional" is basically the ternary operator, but with the condition in the middle instead of the first operand, which is pretty unique among languages.
Python has always had this thing of writing expressions (especially conditionals) after one would expect them, based on the experience with other languages, but in the end they all make (at least some) sense.
I'd add an one line conditional counting:
Since this creates a generator and then sums over it, it avoids creating an intermediary copy of the items in the list that satisfy the condition.
I could never understand why pythonistas (is that the right term?) like list comprehension. It seems much more confusing to me than the functional map pattern. And aside from sum, there aren't great patterns for reduce or fold!
I think list comprehensions feel more pythonic. The syntax fits perfectly with the rest of python. I also think it is more similar to natural languages.
Example:
"Please give me the
dog names
for everydog
from myanimals
(if it really is a dog)"It is also easy to use the same syntax for dictionary comprehensions and generators. Especially generators are a useful concept. Instead of creating a new list in memory they can generate them on the fly:
Instead of downloading the complete internet into your memory this will download the pages one by one while iterating the generator.
These are the reasons why I personally love the comprehension syntax in python.
Edit
Uhm I just noted that your comment is already a year old. Sorry for reviving an old conversation.
Think of it as a gateway to functional programming. It mostly looks like a compact version of a for loop, so it's easier for folks who only know procedural or oo.
for the second example I prefer:
There were several concepts in Python that were not so obvious for me. Few of them are : Lists and Dictionary Comprehensions, One line conditional operator, generators, range in Python 3, writing if statements like if 5<a<8:, tuple assignment and unpacking, the use of ** operator..
Now I have learned a thing or two in this post. Thank you
Thank you for this - I'll bear it in mind when I start learning Python later this year.