[SOLVED]Why are the loop variables for k and v used in a loop to update a dictionary still defined outside the loop?(Python)
Hi there. I bumped into a code in DataCamp when I googled stg about nested dict comprehensions, and they put the -almost- equivalent for loop for the comprehension there, too. So, given nested_dict = {'first': {'a': 1}, 'second': {'b': 2}} To turn this into a dictionary with the numbers converted to floats, they did: for (outer_k, outer_v) in nested_dict.items(): for (inner_k, inner_v) in outer_v.items(): outer_v.update({inner_k: float(inner_v)}) nested_dict.update({outer_k: outer_v}) # THIS print(nested_dict) which then outputs the desired result. Now the line I marked with "THIS" is what bewilders me. How come loop vars can exist outside the loop? I'm sorry if this is sounding stupid, I had to take a break from python for some time. Lesson learned. But still, it feels as though this is something new to me. Is this due to update()? Thank you. https://code.sololearn.com/crDb301kPSS6/?ref=app