Original Sources:
Used in this file:
~ Ex.:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"] Instead of doing:
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x) a better way is:
generator = x for x in fruits if "a" in x
newlist = list(generator)Prefer Generators
In the two examples before the last return a generator, usually prefered to a for loop.
How about a filter?
I personally recommand using the filter function instead, it is more functional, the Python PEP-8 prefer the list comprehension.