DEV Community

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

Edgar Darío on December 21, 2017

I introduced myself in the programming world using very basic languages. Since 4 years ago, I created many many projects using Python, but although...
Collapse
 
ben profile image
Ben Halpern

Nice 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
 
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
 
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
 
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.

Collapse
 
skyxie_60 profile image
skyxie

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!

Collapse
 
winstonyallow profile image
Winston • Edited

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:

dog.name for dog in animals if dog.type == "dog"

"Please give me the dog names for every dog from my animals (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:

websites = (download(url) for url in the_complete_internet)

for page in websites:
    print(page)

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.

Collapse
 
rascalking profile image
David Bonner

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.

Collapse
 
connectety profile image
Connectety

for the second example I prefer:

b = filter(lambda i: i >= 15, a)
Collapse
 
5hraddha profile image
Shraddha • Edited

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..

Collapse
 
jibolaoseni profile image
jibolaoseni

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

Collapse
 
dominicduffin1 profile image
Dominic Duffin

Thank you for this - I'll bear it in mind when I start learning Python later this year.

Collapse
 
mufaddal1165 profile image
Mufaddal

wasn't aware of the one line conditionals.

Collapse
 
blackbird profile image
Omkar Ajnadkar

Nice post!

Collapse
 
tunaxor profile image
Angel Daniel Munoz Gonzalez

Great post and Great answers!

Collapse
 
enzoftware profile image
Enzo Lizama Paredes

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

Collapse
 
fannyvieira profile image
Fanny

Great post, Darío! Thanks! <3

Collapse
 
ravinduf profile image
Ravindu Senal Fernando

👍👌👌

Collapse
 
staceyjovcic profile image
staceyjovcic

Great post!

Collapse
 
rpalo profile image
Ryan Palo

This is an awesome little post!

Collapse
 
xeonzolt profile image
Harsh Lathwal • Edited

some = ["Nope","Dafaq"][x < 56]

Collapse
 
xanderyzwich profile image
Corey McCarty

There are lots of abuses shoving too much into one line, but these are nice and easy.

Collapse
 
rozzs74 profile image
John Royce

I love list comprehension in some way but I wouldn't exchange it for readability purposes. (y) It actually make the code itself flattened but readability first "Explicit is better than implicit."

Collapse
 
rezald_codetmen profile image
Rezald

Can't wait for the next chapter. Nice post!

Collapse
 
a3n3d3i profile image
Andi • Edited

init file in a folder and init function in a class... why naming them the same?!