I am trying to implement an algorithm in Python to generate all Permutations of a list. But I In my for loop I wish to keep the original prefix and rest lists intact, and therefore I am trying to make a copy of those lists using newprefix and newrest, however on printing the variable rest at each iteration, I see that even the variable rest is getting modified! How can I make a shallow copy of the list in Python? Or is there another issue with my attempted logic?
def perm(prefix, rest):
if len(rest) == 0:
print prefix
for i in range(len(rest)):
#prints in the for loop are just for debugging
print "rest:", rest
print "i=", i
newprefix = prefix
newprefix.append(rest[i])
newrest = rest
newrest.pop(i)
print "old pre : ", prefix
print "newpre=", newprefix
print "newrest=", newrest
perm(newprefix, newrest)
perm([], ['a','b','c'])