Is it possible to check in python if an item is in a dictionary of items in a way like below:
for x in rows:
print(x.items())
for item in seq.items():
print(item)
if item in x.items():
print("got it")
Rows = [{'name': 'Alice', 'AGATC': '2', 'AATG': '8', 'TATC': '3'},
{'name': 'Bob', 'AGATC': '4', 'AATG': '1', 'TATC': '5'},
{'name': 'Charlie', 'AGATC': '3', 'AATG': '2', 'TATC': '5'}]
Seq = {'AGATC': 4, 'AATG': 1, 'TATC': 5}
I know that I can check if value exist but I want check if whole item exists. It will be an easier way to do this.
Assuming you just want to loop through the rows and then loop through the items that you're looking for and check if any of those items are in a row... yes, that is very possible:
This way you're picking a row and checking if any of the items from
seqcan be found in the row. The finding itself checks whether the whole item exists, just like you want, by callingrow.get(key)first (and checking if they key exists in the first place) and then by comparing it to the value you want it to be equal.We're lucky that
.get(key[, default])as such you don't need to worry about crashing when comparing values of non-existent keys.
Instead of
any(), you could also keep your 2nd loop and know exactly which item was found if you care about that information.