How do I check for a specific letter inside a value in a dictionary when there are other letters/numbers in it?

Viewed 29

Looking at the dictionary below, let's say I wanted to check if there's a d in the entire dictionary, it won't let me do that because there are other numbers besides it, how do I tell my code to ignore them?


view_list = {'24169': '11h', '31254': '14h', '16155': '1d', '4565': '1d', '165929': '2d', '12906': '3d'}
                    
                    for letter in view_list:
                        if "w" in view_list.values():
                            print("week")
                    
                        if "d" in view_list.values():
                            print("day")
2 Answers

You can iterate over the values of the dictionary using .values() as:

view_list = {'24169': '11h', '31254': '14h', '16155': '1d', '4565': '1d', '165929': '2d', '12906': '3d'}

for val in view_list.values():
    if "w" in val:
        print("week")
    if "d" in val:
        print("day")

Output:

day
day
day
day

This might be a "pythonic" solution:

any('d' in v for k,v in view_list.items())
Related