List comprehensions Python

All, Tips

I think one of the most beautiful things and also powerful are the list compreehensions, which makes the language quite interesting.

The syntax is quite simple:

[ expression for item in list if conditional ]

The tutorial [1] explains specifically how to do it. I confess it’s not easy to take it at first try. You can also use directly from the source [3].

But when you learn it, you will want to use all over your code. EVEN WHEN YOU SHOULDN’T!

In some cases, when you dont need a list actually, its better to use a simple NEXT() function[2].

[1] https://www.pythonforbeginners.com/lists/list-comprehensions-in-python/

[2] https://docs.python.org/2/library/functions.html

[3] https://docs.python.org/2/tutorial/datastructures.html

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s