DEV Community

Edgar Darío
Edgar Darío

Posted on

Things that weren't so obvious when you started to program in Python

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"
Enter fullscreen mode Exit fullscreen mode

Nothing simpler than putting:

some = "Dafaq" if x < 56 else "Nope"
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Best way:

a = [2, 24, 88, 32, 1, 6, 88, 10, 15, 34] # example list.
b = [i for i in a if i >= 15]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Best way:

amounts = [2, 24, 88, 32, 1, 6, 88, 10, 15, 34]
total = sum(amounts)
Enter fullscreen mode Exit fullscreen mode

In the next chapter...

Things that weren't so obvious when you starting to using Django...

Oldest comments (27)

Collapse
 
ben profile image
Ben Halpern

Nice post!

Collapse
 
rpalo profile image
Ryan Palo

This is an awesome little post!

Collapse
 
tterb profile image
Brett Stevenson

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.

Collapse
 
enzoftware profile image
Enzo Lizama Paredes

That's exactly what happens to me when I started. Lovely <3

Collapse
 
maxart2501 profile image
Massimo Artizzu

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.

Collapse
 
staceyjovcic profile image
staceyjovcic

Great post!

Collapse
 
emcain profile image
Emily Cain • Edited

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:

squares = [i**2 for i in integers]
titles = [b.title for b in books]

etc.

You can also make dictionary comprehensions.

A related concept is range:

for i in range(0, 10): 
    print(i)

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.

Collapse
 
danielz_ profile image
DanBot5K

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.

Collapse
 
jibolaoseni profile image
jibolaoseni

Now I have learned a thing or two in this post. Thank you

Collapse
 
blackbird profile image
Omkar Ajnadkar

Nice post!

Collapse
 
kmyokoyama profile image
Kazuki Yokoyama • Edited

I'd add an one line conditional counting:

amounts = [2, 24, 88, 32, 1, 6, 88, 10, 15, 34]
greater_than_15 = sum(1 for amount in amounts if amount > 15)

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.