Having some trouble getting this simple conditional to work. I have a dictionary with 10 key-value pairs as follows:
JavaScript
x
2
1
{0.1: 0.91, 0.2: 0.701, 0.3: 0.522, 0.4: 0.203, 0.5: 0.042, 0.6: 0.004, 0.7: 0.0, 0.8: 0.0, 0.9: 0.0, 1.0: 0.0}
2
I’m trying to loop through the dictionary and print the key for the first value that is below 0.05.
This is the code I have right now:
JavaScript
1
10
10
1
i = 0
2
length = len(list(adf_dict.items()))
3
keys, values = list(adf_dict.keys()), list(adf_dict.values())
4
5
while i < length:
6
if values[i] < 0.05:
7
print(key[i])
8
else:
9
break
10
Can anyone help me see where I’m going wrong here?
Edit: It should be noted that I only want to print the first key where the condition is met and then end the loop.
Advertisement
Answer
This is what your looking for:
JavaScript
1
5
1
for key, value in adf_dict.items():
2
if value < 0.05:
3
print(key)
4
break
5