Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If withing lists
#1
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
Reply
#2
pseudo code:
for eachdict in dictlist:
    for key, value in eachdict.items():
        if value == keyword:
           ...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,433 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,318 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020