Hussein
Hussein

Reputation: 97

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

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")

Upvotes: 1

Views: 36

Answers (2)

Krishna Chaurasia
Krishna Chaurasia

Reputation: 9590

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

Upvotes: 1

anon01
anon01

Reputation: 11181

This might be a "pythonic" solution:

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

Upvotes: 0

Related Questions