How to make a shallow copy of a list in Python

To make a shallow copy, you can slice the list:

newprefix = prefix[:]

Or pass it into the list constructor:

newprefix = list(prefix)

Also, I think you can simplify your code a little:

def perm(prefix, rest):
    print prefix, rest

    for i in range(len(rest)):
        perm(prefix + [rest[i]], rest[:i] + rest[i + 1:])

perm([], ['a','b','c'])

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)