Udacity CS 212: Learning more

A new toy we can play with, itertools.

The module itertools include methods to build iterators effectively. Its pretty hard to understand this description from the Python documentation, so we’ll just look at some examples from the website:

repeat(1,5) # object representing (1, 1, 1, 1, 1)
imap(func,(1,2,3),(4,5,6)) # object representing (func(1,4), func(2,5), func(3,6))
product('ABC','xy') # ('Ax', 'Ay', 'Bx', 'By', 'Cx', 'Cy')
permutations('ABC', 2) #('AB', 'AC', 'BA', 'BC', 'CA', 'CB')
combinations('ABC', 2) # ('AB', 'AC', 'BC')

Do remember to import itertools and prefix every method you want to use with itertools.

A very important concept to know is list comprehension.

[s for r,s in cards if s == 'H']
# this has the format [(term) for (for clause) ...optional if and for clauses]

The expression above is basically the same as the block below:

result = []
for r,s in cards:

if s == 'H':


The benefits of writing list comprehension is that it is simple to add more for and if conditions, just add it to the back of the line. The entire list comprehension is read from left to right, ignoring the first term.

A very similar concept generator expression.

The format of a generator expression is very similar to that of a list comprehension:

g = (s for r,s in cards if s == 'H')

Note the differences, using parentheses instead of square brackets. A generator does not evaluate the expression in the parentheses when you first assign it. Instead it represents a generator object, which is sort of like an understanding that g will evaluate the expression when called to. So the way to ask g to start evaluating is next(g). This will get the first s (since it is still on the 0th place as g has not started to evaluate. The next of 0th place is the first term)

A simpler way to iterate through a generator to print out every term in the expression is:

for i in g: print i

This ensures that you will not iterate through the end of g (by manually calling next), which will cause Python to throw a StopIteration error.
Alternatively, you can convert the expression into a list using list(g), this will return a list containing each term of g as an element.

A cool looking thing *args

You usually see *args in definitions:

def something(fn, *args):


In the definition, *args means that the function can take any number of arguments, and all the arguments should be joined together into a tuple called *args.
And when it appears in the block, it means to unpack the tuple, and apply the function fn to all the elements in the tuple.

The interesting thing to note here is that functions can be passed around as arguments, like strings and integers. So we are able to decide when to execute the functions, which is useful to checking how long a particular function takes to run, by calling time.clock() once before running, and again after running.

This Udacity post references Udacity CS 212 Unit 2.

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