Python Forum
Searching through a list of dictionaries with a condition.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searching through a list of dictionaries with a condition.
#10
a_list=[{"Sample" : "A-15", "Run" : "n47", "quality" : "good" }, 
{"Sample" : "B-04", "Run" : "n45", "quality" : "good"}, 
{"Sample" : "C-10", "Run" : "n48", "quality" : "bad"}, 
{"Sample" : "Z-95", "Run" : "n47", "quality" : "good" },]

sampleIDs=['A-15', 'B-04', 'C-10']

for each_dict in a_list:
	if each_dict['Sample'] in sampleIDs:
		print(each_dict['Sample']+" "+each_dict['Run'])
So if I run this^ I expect to get:
A-15 n47
B-04 n45
C-10 n48


but instead I get:
C-10 n48

Is this because I'm overwriting the operation with each iteration?
If so how can I avoid doing that?
Reply


Messages In This Thread
RE: Searching through a list of dictionaries with a condition. - by Mr_Keystrokes - Oct-04-2018, 01:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 2 656 Apr-29-2024, 04:38 PM
Last Post: Calab
  unable to remove all elements from list based on a condition sg_python 3 503 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Access list of dictionaries britesc 4 1,127 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,561 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Searching in the list quest 4 1,586 Mar-02-2022, 10:30 AM
Last Post: quest
  function that returns a list of dictionaries nostradamus64 2 1,792 May-06-2021, 09:58 PM
Last Post: nostradamus64
  convert List with dictionaries to a single dictionary iamaghost 3 2,897 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,342 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138
  Help accessing elements of list of dictionaries Milfredo 6 2,896 Sep-07-2020, 01:32 AM
Last Post: Milfredo
  Accessing values in list of dictionaries pythonnewbie138 2 2,166 Aug-02-2020, 05:02 PM
Last Post: pythonnewbie138

Forum Jump:

User Panel Messages

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