Nevaeha
Nevaeha

Reputation: 115

Finding keywords in lists inside a dictionary

I have a dictionary that will have multiple dictionary / lists inside of it and I want to know how to go through each and search for keywords and add them total.

Example dict1 = { "subDict1": "list1": [ "yes", "yes", "no" ] } }

Assume I had 10 sublists inside this dictionary. How can I go through each list to count the total yes that I have?

I assume I need to do something similar to this

yes = 0
for d in dict1:
    for yes in dict1["subDict1"]
        yes +=1
print(yes)

Whenever I print it is always 0, and always runs like 108 times. but thats about where I am stuck trying to figure out the rest.

I was reading over this guide, but it doesnt seem to quite answer the question.

Upvotes: 0

Views: 113

Answers (4)

ObSp
ObSp

Reputation: 31

There's a lot of different ways to go about this. If you'd like the simplest and easiest to understand, here's all you need to do:

#assuming dict1 = { "subDict1": {"list1": [ "yes", "yes", "no" ]} }
count = 0

for subDict in dict1.values(): #iterate through sub dictionaries of dict1
    for l in subDict.values(): #iterate through the lists in the sub dict
        for item in l: #iterate through the items in the list
            if item == "yes": count += 1 #if the item matches, add 1 to our count

print(count)

This should be pretty simple to understand if you're just getting started with Python, but I do recommend the methods shown above as it's less code that you need to write and better practice.

Upvotes: 2

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15728

dict1 = { "subDict1": { "list1": [ "yes", "yes", "no" ] } }
counter = 0

for subdict in dict1.values():
    for l in subdict.values():
        counter += l.count("yes")

print(counter)

or if you want a one liner:

counter = sum(v.count("yes") for subdict in dict1.values() for v in subdict.values())
print(counter)

Upvotes: 1

Suramuthu R
Suramuthu R

Reputation: 1913

Assuming that you have the dict in the following form, I've given the code.

dict1 = {
        "subDict1": {"list1": [ "yes", "yes", "no" ] },
        "subDict2": {"list2": [ "no", "yes", "no" ] },
        "subDict3": {"list3": [ "yes", "yes", "yes" ] },
        "subDict4": {"list4": [ "no", "no", "no" ] }
         }

yes = 0

#iterate thru values of element of dict1
for d in dict1.values():

        #iterate thru values of previously iterated values
    for x in d.values():

        yes = yes + x.count('yes')
print(yes)  # Output : 6

Upvotes: 2

max
max

Reputation: 83

This approach should work:

dict1 = { "subDict1": [ "yes", "yes", "no" ] } 
string_to_find = 'yes'
string_occurence = 0
for key, value in dict1.items():
    string_occurence += value.count(string_to_find)
print(string_occurence)

The reason that it showed 0, was that you ran through the dict itself, not the value, the lists.

Also you can not search in the dict with the variable name yes itself, you need to define it as string.

With list.count(string) you can also directly count all occurences of an item inside a list. You can find more about python data structure operations here: https://docs.python.org/3/tutorial/datastructures.html

Upvotes: 3

Related Questions