NameError from Python input() function

input reads and evaluates a Python expression. When it tries to evaluate it, it looks for a variable e, which is not defined, and fails. You almost always want to use raw_input instead. (And in Python3, input has this behaviour.) Or, better, on Unix, use readline so the user can edit their input.

How to generate all permutations of a list?

There’s a function in the standard-library for this: itertools.permutations. If for some reason you want to implement it yourself or are just curious to know how it works, here’s one nice approach, taken from http://code.activestate.com/recipes/252178/: A couple of alternative approaches are listed in the documentation of itertools.permutations. Here’s one: And another, based on itertools.product: