Search code examples
loopsdictionarykey

Why does my loop that points to the Dictionary keep printing the same value?


Why do I make a loop I don't get : Cell_num_1 Cell_num_3 Cell_num_5 Cell_num_7

i get always "Cell_num_8"

Admin = {
    "Cell_num_1": "NaN",
    "Cell_num_2": 987654321,
    "Cell_num_3": "NaN",
    "Cell_num_4": 123456789,
    "Cell_num_5": "NaN",
    "Cell_num_6": 987654321,
    "Cell_num_7": "NaN",
    "Cell_num_8": 123456789,
}

for values in Admin.values():
    if values =="NaN":
        print (keys)

Solution

  • Since you are only looping through values, you can not access the keys in dictionary. Try the following:

    for key,value in Admin.items():
        if value == "NaN":
            print(key)