I am new to Python, and trying to learn it "on the job". And I am required to do this.
Is it possible to create a 'dictionary1' dynamically which takes another 'dictionary2' as value, where 'dictionary2' is also getting updated in every for loop. Basically in terms of code, I tried:
fetch_value = range(5) #random list of values (not in sequence)
result = {} #dictionary2
ret = {} #dictionary1
list1 = [0, 1] #this list is actually variable length, ranging from 1 to 10 (assumed len(list1) = 2 for example purpose only)
for idx in list1:
result[str(idx)] = float(fetch_value[1])
ret['key1'] = (result if len(list1) > 1 else float(fetch_value[1])) # key names like 'key1' are just for representations, actual names vary
result[str(idx)] = float(fetch_value[2])
ret['key2'] = (result if len(list1) > 1 else float(fetch_value[2]))
result[str(idx)] = float(fetch_value[3])
ret['key3'] = (result if len(list1) > 1 else float(fetch_value[3]))
result[str(idx)] = float(fetch_value[4])
ret['key4'] = (result if len(list1) > 1 else float(fetch_value[4]))
print ret
This outputs to:
{'key1': {'0': 4, '1', 4}, 'key2': {'0': 4, '1', 4}, 'key3': {'0': 4, '1', 4}, 'key4': {'0': 4, '1', 4}}
What I need:
{'key1': {'0': 1, '1', 1}, 'key2': {'0': 2, '1', 2}, 'key3': {'0': 3, '1', 3}, 'key4': {'0': 4, '1', 4}}
anything obvious I am doing wrong here?