Im trying to add elements to a dict list (associative array), but every time it loops, the array overwrites the previous element. So i just end up with an array of size 1 with the last element read. I verified that the keys ARE changing every time.
array=[]
for line in open(file):
result=prog.match(line)
array={result.group(1) : result.group(2)}
any help would be great, thanks =]
Your solution is incorrect; the correct version is:
array={}
for line in open(file):
result=prog.match(line)
array[result.group(1)] = result.group(2)
Issues with your version:
This is like saying:
array={result.group(1) : result.group(2)}
array={'x':1}
array={'y':1}
array={'z':1}
....
array remains one element dict
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