I want to check if two items are in a list. But the method should follow a kind of order.
Here, I have a dictionary of people along with their favorite numbers. But the numbers are the keys and the people are the values.
fav_num_dict = {13: 'A', 33: 'B', 23: 'C', 25: 'D', 11: 'E', 4: 'F'}
people_to_check = ["D", "B"]
for num, person in fav_num_dict.items():
if person in people_to_check:
print(fav_num_dict[num], num)
I am doing some kind of work where the order of the people in people_to_check
matters. But when I run the script, it prints "B", with his/her number first and then "D". Now, that happens because "B" comes first in the dictionary and it also exists in people_to_check
despite as the second item.
My question here is how do I make the program respect my order for people_to_check
? Like, if it finds "B" first but it is the second item in my specified list, it should wait until it finds the first item, "D" and then "B".
I can also do a people_to_check.reverse()
but that always won't work. Because people_to_check
can also have items that are in order with the items in fav_num_dict