user13557808
user13557808

Reputation:

Return the dictionary from multiple lists of dictionaries with the highest value in a specific key

I have the following two lists of dictionaries:

    regionA = [dict(is_buy_order=True, price=1000, type=1),
               dict(is_buy_order=True, price=100, type=2),
               dict(is_buy_order=False, price=10, type=2)]
    regionB = [dict(is_buy_order=False, price=10, type=1),
               dict(is_buy_order=True, price=100, type=1),
               dict(is_buy_order=True, price=1000, type=2)]

I would like to return the dictionaries with the highest value in the key ‘price’ and with the value ‘True’ in the key ‘is_buy_order’ for both type 1 and type 2, taking in account data from both regionA and regionB.

I have seen examples (e.g. Return the dictionary from a list of dictionaries with the highest value in a specific key) that work for a single list of dictionaries but cannot make them work with a set, I was wondering whether there is a direct way to do it.

(first question here: let me know if it needs clarification, and thanks for your help!)

Upvotes: 0

Views: 64

Answers (6)

angeldeluz777
angeldeluz777

Reputation: 339

You can join the lists first:

max([d for d in regionA + regionB if  d['is_buy_order']], key=lambda d: d['price']))

Upvotes: 0

Alfred Ayi-bonte
Alfred Ayi-bonte

Reputation: 775

regionA = [dict(is_buy_order=True, price=1000, type=1),
  dict(is_buy_order=True, price=100, type=2),
  dict(is_buy_order=False, price=10, type=2)]

regionB = [dict(is_buy_order=False, price=10, type=1),
               dict(is_buy_order=True, price=100, type=1),
               dict(is_buy_order=True, price=1000, type=2)]

c = regionA + regionB
type1 = dict(price=0)
type2 = dict(price=0)
for item in c:
  if (type1["price"]  < item["price"]) and item["is_buy_order"] and item["type"]==1:
    type1 = item
  if (type2["price"] < item["price"]) and item["is_buy_order"] and item["type"]==2:
    type2 = item

print('type1 max price', type1)
print('type2 max price', type2)

Upvotes: 1

Willian Lopes
Willian Lopes

Reputation: 46

You can concatenate the dictionaries by doing regionC = regionA + regionB.

Then, you can apply the following:

 type_1 = max((x for x in regionC if x['is_buy_order'] and x['type'] == 1), key=lambda x: x['price'])
print(type_1)
 type_2 = max((x for x in regionC if x['is_buy_order'] and x['type'] == 2), key=lambda x: x['price'])
print(type_2)

The output will be:

{'is_buy_order': True, 'price': 1000, 'type': 1}
{'is_buy_order': True, 'price': 1000, 'type': 2}

If you do not want to consider the type, you can just remove it from the list comprehension. Then, it will be

 type_1 = max((x for x in regionC if x['is_buy_order']), key=lambda x: x['price'])

Upvotes: 0

Bhaskar
Bhaskar

Reputation: 710

This should work. First I concatenate the lists, regionA and regionB, into a single list, total_data. Then for each type, I find the maximum price out of all orders that satisfy the constraint that is_buy_order is true and type is the respective type.

total_data = regionA + regionB    
maxType1 = max([order['price'] for order in total_data if order['is_buy_order'] and order['type'] == 1])
maxType2 = max([order['price'] for order in total_data if order['is_buy_order'] and order['type'] == 2])

Upvotes: 0

Umar Aftab
Umar Aftab

Reputation: 535

You can convert the set of dictionaries into a pandas dataframe, it will make your life much easier : (This is not complete code but along those lines ...)

import pandas as pd
Region = dict(regionA,regionB)
df = pd.DataFrame(Region, columns= ['Region','BuyOrder', 'Price','Type'])
df.sort_values(by=['Price'], inplace=True)

Upvotes: 1

revliscano
revliscano

Reputation: 2272

You can do it by doing something as follows:

both_regions = regionA + regionB
result = sorted([d for d in both_regions if d['is_buy_order']],
                key=lambda x: x['price'], reverse=True)[0]

output

{'is_buy_order': True, 'price': 1000, 'type': 1}

Upvotes: 1

Related Questions