Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

All permutations of a list in Python (See related posts)

This function returns a list containing all permutations of the input list. Does not work directly on strings -- explode the string into a list of characters to do that.

def perm(l):
    sz = len(l)
    if sz <= 1:
        return [l]
    return [p[:i]+[l[0]]+p[i:] for i in xrange(sz) for p in perm(l[1:])]

You need to create an account or log in to post comments to this site.


Click here to browse all 5141 code snippets

Related Posts