Building Lists with List of Comprehension

PHOTO EMBED

Mon Feb 28 2022 08:37:07 GMT+0000 (Coordinated Universal Time)

Saved by @ghefley #python #lists #arrays

## Instead of building a list with a loop:

b = [] 
for x in a: 
    b.append(10 * x) 
foo(b) 

## you can often build it much more concisely
 ## with a list comprehension:

foo([10 * x for x in a]) 

##or, if foo accepts an arbitrarily iterable (which it usually will), ## a generator expression:

foo(10 * x for x in a) 
content_copyCOPY

https://www.quora.com/What-are-some-cool-Python-tricks