ECE 2524 - Common Python Idioms

ECE 2524

Introduction to Unix for Engineers

Common Python Idioms

Transform a list of items from one form to another

Whenever you have something that looks like

collection2 = [] # a new, empty list
for thing in collection1:
    collection2.append( some_function( thing ) )

you can use list comprehension to write it as

collection2 = [ some_function(thing) for thing in collection1 ]