geotom / free.dm-Common

Core framework of free.dm
MIT License
1 stars 1 forks source link

Use generator expressions instead of comprehension where possible #3

Open geotom opened 6 years ago

geotom commented 6 years ago

If we need to iterate only once, it is better to use a generator expression instead of a normal list comprehension!

Generator expression

(x*2 for x in range(256))

List comprehension

[x*2 for x in range(256)]

By using list(generator_expr) we would get the same result when needed.

Check the code where we use comprehensions and replace these when possible (https://medium.freecodecamp.org/python-list-comprehensions-vs-generator-expressions-cef70ccb49db). Also look at Async comprehensions introduced with python 3.6 https://www.blog.pythonlibrary.org/2017/02/14/whats-new-in-python-asynchronous-comprehensions-generators/