Apex
Apex

Reputation: 1096

print dictionary element in a list of dictionaries using python

I have a list that contains several dictionaries - The list looks like this:

[{'DeltaG': -9.82, 'BasePairs': 4}, {'DeltaG': -9.25, 'BasePairs': 6}, {'DeltaG': -8.96, 'BasePairs': 8}]

What I want to do is to print DeltaG value if the BasePairs == 6 is found. If the BasePairs == 6 is NOT found then print the deltaG for the first BasePairs < 6:

I tried the code below but it gives the DeltaG= -9.82 which is for the first BasePairs == 4 while it should first search if there is BasePiars== 6 first.

for tuple in results:
    if float(tuple["BasePairs"]) == 6:  
        deltaG = float(tuple["DeltaG"])
print(deltaG)
    else:
        if float(tuple["BasePairs"]) < 6: 
            deltaG = float(tuple["DeltaG"])
print(deltaG)

Anyone can help me with this? Thank you

Upvotes: 0

Views: 84

Answers (3)

nico9T
nico9T

Reputation: 2716

I'd use a list comprehension to get all the BasePairs values then I'd look if 6 (threshold) is inside it. If yes I can get its index and access the 'DeltaG' value for it otherwise I return the first 'DeltaG' where value < threshold.

Code:

results = [{'DeltaG': -9.82, 'BasePairs': 4}, 
            {'DeltaG': -9.25, 'BasePairs': 6}, 
            {'DeltaG': -8.96, 'BasePairs': 8}]
    
threshold = 6
    
x = [d['BasePairs'] for d in results]

if threshold in x: 
    print(results[x.index(threshold)]['DeltaG'])
else:
    i = 0
    for v in x:
        if v < threshold:
            print(results[x.index(v)]['DeltaG'])
            break
        i +=1

Output:

-9.25

Let's try different values. I modified the first 'BasePairs' to 18, then I try different threshold values.

results = [{'DeltaG': -9.82, 'BasePairs': 18}, 
            {'DeltaG': -9.25, 'BasePairs': 6}, 
            {'DeltaG': -8.96, 'BasePairs': 2}]

threshold = 8

-9.25

threshold = 5

-8.96

Upvotes: 0

iGian
iGian

Reputation: 11183

Starting from your code, you can check something like this:

found = False
for tup in results:
    if float(tup["BasePairs"]) < 6 and not found: 
        deltaG = float(tup["DeltaG"])
        found = True
    if float(tup["BasePairs"]) == 6:  
        deltaG = float(tup["DeltaG"])
        break
print(deltaG)

Or if you want to use the smaller value:

min_base_pairs_val = float('inf')
for tup in results:
    if float(tup["BasePairs"]) < min_base_pairs_val: 
        deltaG = float(tup["DeltaG"])
        min_base_pairs_val = float(tup["BasePairs"])
    if float(tup["BasePairs"]) == 6:  
        deltaG = float(tup["DeltaG"])
        break
print(deltaG)

Note, don't use tuple as variable name, as it is a function in Python.

Upvotes: 1

Sayse
Sayse

Reputation: 43300

You can use next with a default

next(
   (i["DeltaG"] for i in results if i["BasePairs"] == 6),
   next(
       (i["DeltaG"] for i in results  if i["BasePairs"] < 6), None
   )
)

This searches for the first value in your list of dictionaries that has a base pairs of 6, if it doesnt find any, it returns the first value with a base pair less than 6 (or None if none found)

Upvotes: 2

Related Questions