0

I have a Python code, in which I desire to group the values by their corresponding labels

labels = [0, 0, 0,  1, 1,  2]
values = [1, 3, 5,  6, 7,  8]    

groups = [[]]*3

for i in range(len(values)): 
    groups[labels[i]] += [values[i]]

print(groups)

I expect to get

[[1,3,5], [6,7], [8]]

But, what I get from the code is

[[1, 3, 5, 6, 7, 8], [1, 3, 5, 6, 7, 8], [1, 3, 5, 6, 7, 8]]

So, what's wrong in my code?

Leonard Neon
  • 183
  • 1
  • 7

0 Answers0