This is my code:
a = [[]] * 10
a[0].append(1)
print a # Outputs [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]
How can I get a to output
[[1], [], [], [], [], [], [], [], [], []]
?
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.
Try
In your code you’re adding the same list 10 times. The following output should clarify this:
As you can see, in the first example
acontains 5 times a reference to the same array object, in the second example it contains references to 5 different array objects.