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.
#1
Hey guys,

I have a huge spreadsheet that I am attempting to search through for some specific data.

On the one hand I have IDs like this:

Y00988-11
G01024-14
Z01933-13

And on the other hand I have a massive spreadsheet(CSV) in the following format:

Run,Sample,Source,Rate,
DFT,G01024-14,A,High
DFT,U04424-15,B,Low
TFF,T64673-18,A,Low
RRT,I01324-14,A,High
RRT,J01624-14,A,High
...

I'm trying to extract both the 'Sample' ID and the 'Run'.

I read the csv spreadsheet into a Dictionary using the in built reader, but I'm having trouble extracting the elements I am interested in.

import csv
import sys

# sequences of interest
dataset=sys.argv[1]

# CSV spreadsheet
database=sys.argv[2]

sampleIDs=[]
with open(dataset, 'r') as file:
	for line in file:
		line.strip('\n')
		sampleIDs.append(line)
file.close()

seq_Dict=[]
finalList=['init']


with open(database, 'rb') as csvfile:
	reader=csv.DictReader(csvfile, delimiter='\t')
	for line in reader:
		seq_Dict.append(line)
csvfile.close()


for element in seq_Dict:
	for key, value in element.items():
		if element['Sample'] in sampleIDs:
			finalList.pop()
			finalList.append(element['Sample']+" "+element['Run'])

for i in finalList:
	print(i)
This script returns the info of the last ID in my sampleIDs, so I can see that what is occurring during the loop is being overwriting the previous iteration.
So I did try to use deepcopy but that didn't seem to work.
Reply
#2
It's not overwriting, you are removing the last iteration before you add a new one. The pop method removes the last item of the list. You keep removing an item and adding an item (lines 31 + 32), so you end up with one item. Remove line 31 and it should work.

You can also remove line 25. The with statement on line 21 takes care of that.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Sep-28-2018, 12:07 PM)ichabod801 Wrote: It's not overwriting, you are removing the last iteration before you add a new one. The pop method removes the last item of the list. You keep removing an item and adding an item (lines 31 + 32), so you end up with one item. Remove line 31 and it should work.

You can also remove line 25. The with statement on line 21 takes care of that.

On the contrary, by removing the pop method it still returns the last sample ID, the only difference is that it's repeated by the number of key-values there are in the dictionary i.e.
S01933-11 r480
S01933-11 r480
S01933-11 r480
S01933-11 r480
S01933-11 r480
S01933-11 r480
S01933-11 r480
S01933-11 r480
S01933-11 r480
Reply
#4
Okay, this bit:

for element in seq_Dict:
    for key, value in element.items():
        if element['Sample'] in sampleIDs:
            finalList.append(element['Sample']+" "+element['Run'])
The second for loop is not necessary. What the above code does is for every key in element, it checks element and appends the sample and run. If you get rid of the second for loop, it will just check each element once.

I think there may only be one matching element in the data that matches your filter, and the above issue is why it is repeated. But I can't check that without a (small) sample of the data and what you are passing to sampleIDs.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Hey apologies for late reply, I'm not getting email alerts. I'm going to try what you said, although I'd like to point out why I created the second loop. It's because I don't know what the syntax is to access a particular key in a list of dictionaries.
For example, I know that there is the syntax:
arrayofDict[0]['key']
But this will hone in on only the first element of the list and won't grant access to all the dictionaries in the list. I'm trying to cycle through the list of dictionaries and print out the key-value of a particular key.
Reply
#6
if seq_Dict is your list of dictionaries, that's what your first for loop does. Each time through the loop, element is the next dict in seq_Dict.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Yeah, but the question is, if every dictionary in the list has the same keys-values (structure), can one exclusively access and retrieve the value of the key you're interested in and only that key.
Reply
#8
Sure:

for each_dict in a_list:
     print(each_dict[key])
Most people would do this as a list comprehension:

[each_dict[key] for each_dict in a_list]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Hmm, let me see..
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 453 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Sort a list of dictionaries by the only dictionary key Calab 1 497 Oct-27-2023, 03:03 PM
Last Post: buran
  Access list of dictionaries britesc 4 1,089 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,529 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Searching in the list quest 4 1,538 Mar-02-2022, 10:30 AM
Last Post: quest
  function that returns a list of dictionaries nostradamus64 2 1,765 May-06-2021, 09:58 PM
Last Post: nostradamus64
  convert List with dictionaries to a single dictionary iamaghost 3 2,879 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,306 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138
  Help accessing elements of list of dictionaries Milfredo 6 2,859 Sep-07-2020, 01:32 AM
Last Post: Milfredo
  Accessing values in list of dictionaries pythonnewbie138 2 2,140 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