Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: using only certain keys in a dictionary for a for-loop

Another python-dictionary question :)

here's my dictionary:

mydict = {key1: valueA, key2: valueA, key3: valueB, key4: valueA,
key5: valueA, key6: valueB, key7: valueB, key8: valueA, key9: valueB}

now i want to iterate through the dictionary only for the key-value-pairs of key4, key5, key6 and key7 and check if the value is valueB. i hope it is possible to understand what i mean..

i want to create a for-loop, and only if the value of key4 is valueB, the content of the loop should be executed, then, if the value of key5 is valueB, it should be executed again, and so on. thanks in advance

like image 982
user1106770 Avatar asked Dec 02 '22 01:12

user1106770


1 Answers

for key in [key4, key5, key6, key7]:
    if mydict[key] == valueB:
        pass # do stuff here
like image 89
Tom Whittock Avatar answered Dec 04 '22 08:12

Tom Whittock