1

I am iterating through the keys of one dictionary, finding the same key in a second dictionary, then trying to produce the first value of the list associated with the key in the second dictionary. When I look directly into the second dictionary it works fine:

Code:

for data in hud_data.get('veh_1'):
     print(data[0])

Returns: 17.3245

This is correct. But when I try to run through all of the keys of hud_data by referencing the keys of another dictionary (UAV_data), I get a strange result:

Code:

for a_key in UAV_dict.keys():
    # print(a_key)
    for data in hud_data.get(a_key):
        print(data[0])
        break

This should produce the exact same thing. The first key in UAV_dict is 'veh_1', so when the second for loop runs, it should just return the same thing, 17.3245. Instead it returns all of the values for every key:

Return: 17.3245 19.3003 22.2483 29.8077 35.86

Why are all of the values for every key showing up in the output? How should I re-write the code so that it only produces the first outcome?

PCM
  • 2,881
  • 2
  • 8
  • 30

1 Answers1

0

Your break statement only stops the inner for-loop. The other loop on UAV_dict.keys() is not affected

Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • Ok, that's pretty silly. Thanks. I was running into an issue elsewhere in the code and I thought this was the problem. Obviously this part is ok then. – Michael Wish Nov 24 '21 at 02:26