-1

I have a list of dictionaries (up to 50 dictionaries within this list) and I'm trying to match a dictionary with a specific name, then return a value of another Key/Value pair of that same matched dictionary.

So for example my list:

[{'task': 'clean', 'task_id': 5233', 'state': 'not started'} 

{'task': 'exercise', 'task_id': 2323', 'state': 'started'}

etc 

etc

}]

I want to be able to input a task name key (such as clean) and return the 'task_id' value of that same dictionary (not the task value).

Help would be much appreciated!

Chandella07
  • 2,089
  • 14
  • 22

1 Answers1

0

Correct your json, task_id value should be in proper quotes, then Try this.

record= [{'task': 'clean', 'task_id': '5233', 'state': 'not started'}, 
         {'task': 'exercise', 'task_id': '2323', 'state': 'started'}]


result =[item['task_id'] for item in record if item['task']=='clean']

print(*result)
Chandella07
  • 2,089
  • 14
  • 22