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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With