Python Forum
If withing lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: If withing lists (/thread-14193.html)



If withing lists - 3Pinter - Nov-19-2018

So here I go. I'm going to try to explain this one.

I want to do a check on certain values and based upon its answer doing something.

I have a bunch of items which are dictionaries on their own.

dict = {"test": "yes", "name": "check"} (is way longer but doesn't matter)

so all the items in the list can have different values in that dictionary. ("yes" could be "no" and "name" could be anything.



letters are the dictionaries
list = ["A", "B", "C", "D"]

My goal:
check if "name" contains a certain word in those dictionaries,
--- if true
----- count the occurences:
------- 1? do something
--- if false
----- do something

Using an if-statement doesn't help me (I think) because I need a 'switch construction like in php'?

I hope it makes sense and you could share some help!


ps I can only think of:

countme = []
for i in list and i.name is "keyword":
countme = countme+1
if len(countme):
--- if len(countme) == 1
----- // do something
else
// do something


RE: If withing lists - Larz60+ - Nov-19-2018

pseudo code:
for eachdict in dictlist:
    for key, value in eachdict.items():
        if value == keyword:
           ...