Possible Duplicate:
Python list confusion
I’ve got one little question about Python lists:
Why does this happen?
x = [[]] * 4
x[0].append('x') -> [['x'], ['x'], ['x'], ['x']]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
the same instance of
[]is being duplicated, so when you append to the first one'x', you actually append it to all – because they are all the same object!The right way to do it is to explicitly create a new list instance each time: