Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating replica of data using for loop

I have dictiony which contains 36 data items. I want to replicate each record 100 times. So total records would be 3600.

def createDataReplication(text_list):
    data_item = {}
    print(len(text_list))
    for k,v in text_list.iteritems():
        for i in range(0,100):
            data_item[k+str(i)] = v
    print(len(data_item))

output

36
3510

Why it's 3510 and not 3600? Am I doing any mistake?

like image 895
user2129623 Avatar asked Jan 27 '26 16:01

user2129623


1 Answers

The concatenation k+str(i) is repeated for some combinations of k and i. Dictionary keys must be unique. This causes existing keys to be overwritten.

I suggest you use tuple keys instead which, in addition, aligns data structure with your logic:

for k, v in text_list.iteritems():
    for i in range(100):
        data_item[(k, i)] = v
like image 172
jpp Avatar answered Jan 29 '26 05:01

jpp