[Pythonmac-SIG] permutation of lists

Guido van Rossum guido@CNRI.Reston.VA.US
Thu, 26 Nov 1998 11:59:35 -0500


> def permute(list):
> 	if not list:
> 		return [list]
> 	else:
> 		res = []
> 		for i in range(len(list)):
> 			rest = list[:i] + list[i+1:]
> 			for x in permute(rest):
> 				res.append(list[i:i+1] + x)
> 			return res
> 
> then, I import....
> which gives simply:
> 
> >>> from permcomb import *
> >>> permute([1,2,3])
> [[1, 2, 3]]

Your final 'return' is indented one level too much, so you're only
doing one iteration through the loop.

--Guido van Rossum (home page: http://www.python.org/~guido/)