I have a dict like this
data = {
'a': [95, 93, 90],
'b': [643, 611, 610]
}
I want to iterate over the dict and fetch key and value from list of values for each item, something like this
{'a': 95, 'b': 643}
{'a': 93, 'b': 611}
{'a': 90, 'b': 610}
I have implemented the logic for this and it works fine, but when i see the temp_dict
created in process, i see lots of intermediate unnecessary looping. The end result works just fine but i think it can be improved a lot.
import timeit
data = {
'a': [95, 93, 90],
'b': [643, 611, 610]
}
def calculate(**kwargs):
temp_dict = {}
index = 0
len_values = list(kwargs.values())[0]
while index < len(len_values):
for k, v in kwargs.items():
temp_dict[k] = v[index]
index += 1
yield temp_dict
start_time = timeit.default_timer()
for k in (calculate(**data)):
print(k)
print(timeit.default_timer() - start_time)
How to do it more efficiently?