Python Forum
Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV
#1
Bittrex_Markets = requests.get("https://api.bittrex.com/api/v1.1/public/getmarkets").json()

print (Bittrex_Markets)

>>>


Editing post it was way too long first time
Reply
#2
#!/usr/bin/python3
import requests, pprint

url = "https://api.bittrex.com/api/v1.1/public/getmarkets"

rsp = requests.get(url)
if rsp.status_code == 200:
    json_data = rsp.json()
    pprint.pprint(json_data)
else:
    print("ERROR:", rsp.status_code)
    print(rsp.text)
Output:
{'message': '', 'result': [{'BaseCurrency': 'BTC', 'BaseCurrencyLong': 'Bitcoin', 'Created': '2014-02-13T00:00:00', 'IsActive': True, 'IsRestricted': False, 'IsSponsored': None, 'LogoUrl': 'https://bittrexblobstorage.blob.core.windows.net/public/6defbc41-582d-47a6-bb2e-d0fa88663524.png', 'MarketCurrency': 'LTC', 'MarketCurrencyLong': 'Litecoin', 'MarketName': 'BTC-LTC', 'MinTradeSize': 0.01686767, 'Notice': None }, ... ... ...
Reply
#3
Bittrex_Markets = requests.get("https://api.bittrex.com/api/v1.1/public/getmarkets").json()

Print (Bittrex_Markets)
>>>
{'success': True, 'message': '', 'result': [{'MarketCurrency': 'LTC', 'BaseCurrency': 'BTC', 'MarketCurrencyLong': 'Litecoin', 'BaseCurrencyLong': 'Bitcoin', 'MinTradeSize': 0.01686767, 'MarketName': 'BTC-LTC', 'IsActive': True, 'IsRestricted': False, 'Created': '2014-02-13T00:00:00', 'Notice': None, 'IsSponsored': None, 'LogoUrl': 'https://bittrexblobstorage.blob.core.windows.net/public/6defbc41-582d-47a6-bb2e-d0fa88663524.png'}, {'MarketCurrency': 'DOGE', 'BaseCurrency': 'BTC', 'MarketCurrencyLong': 'Dogecoin', 'BaseCurrencyLong': 'Bitcoin', 'MinTradeSize': 480.76923077, 'MarketName': 'BTC-DOGE', 'IsActive': True, 'IsRestricted': False, 'Created': '2014-02-13T00:00:00', 'Notice': None, 'IsSponsored': None, 'LogoUrl': 'https://bittrexblobstorage.blob.core.windows.net/public/a2b8eaee-2905-4478-a7a0-246f212c64c6.png'}, {'MarketCurrency': 'VDX', 'BaseCurrency': 'ETH', 'MarketCurrencyLong': 'Vodi X', 'BaseCurrencyLong': 'Ethereum', 'MinTradeSize': 300.0, 'MarketName': 'ETH-VDX', 'IsActive': True, 'IsRestricted': True, 'Created': '2019-05-23T00:41:32.987', 'Notice': None, 'IsSponsored': None, 'LogoUrl': 'https://bittrexblobstorage.blob.core.windows.net/public/3277e3cb-fe62-46df-9162-3d9ba67c521e.png'}]}




Bittrex_Get_Market_Summaries = requests.get("https://api.bittrex.com/api/v1.1/public/getmarketsummaries").json()

Print (Bittrex_Get_Market_Summaries)
>>>
{'success': True, 'message': '', 'result': [{'MarketName': 'BTC-DYN', 'High': 1.657e-05, 'Low': 1.437e-05, 'Volume': 83445.47466841, 'Last': 1.468e-05, 'BaseVolume': 1.27935571, 'TimeStamp': '2019-05-28T07:18:48.797', 'Bid': 1.437e-05, 'Ask': 1.461e-05, 'OpenBuyOrders': 42, 'OpenSellOrders': 738, 'PrevDay': 1.445e-05, 'Created': '2017-03-23T00:10:19.403'}, {'MarketName': 'USD-USDT', 'High': 1.009, 'Low': 1.00095, 'Volume': 283483.08497639, 'Last': 1.005, 'BaseVolume': 284948.3504794, 'TimeStamp': '2019-05-28T07:18:48.797', 'Bid': 1.002, 'Ask': 1.00481, 'OpenBuyOrders': 162, 'OpenSellOrders': 97, 'PrevDay': 1.00899, 'Created': '2018-05-31T13:27:08.477'}]}



Bittrex_Mrkt_0 = ((Bittrex_Markets)['result'][0])

Print (Bittrex_Mrkt_0)
>>>
{'MarketCurrency': 'LTC', 'BaseCurrency': 'BTC', 'MarketCurrencyLong': 'Litecoin', 'BaseCurrencyLong': 'Bitcoin', 'MinTradeSize': 0.01686767, 'MarketName': 'BTC-LTC', 'IsActive': True, 'IsRestricted': False, 'Created': '2014-02-13T00:00:00', 'Notice': None, 'IsSponsored': None, 'LogoUrl': 'https://bittrexblobstorage.blob.core.windows.net/public/6defbc41-582d-47a6-bb2e-d0fa88663524.png'}


Bittrex_Mrkt_Summary_0 = ((Bittrex_Get_Market_Summaries)['result'][0])

Print (Bittrex_Mrkt_Summary_0)
>>>
{'MarketName': 'BTC-DYN', 'High': 1.657e-05, 'Low': 1.437e-05, 'Volume': 83445.47466841, 'Last': 1.468e-05, 'BaseVolume': 1.27935571, 'TimeStamp': '2019-05-28T07:18:48.797', 'Bid': 1.437e-05, 'Ask': 1.461e-05, 'OpenBuyOrders': 42, 'OpenSellOrders': 738, 'PrevDay': 1.445e-05, 'Created': '2017-03-23T00:10:19.403'}


if ((Bittrex_Mrkt_0)['IsActive']) == True:
if ((Bittrex_Mrkt_0['IsRestricted'])) == False:
THEN HERE:


I need to return a dictionary from the list of dictionaries in Bittrex_Get_Market_Summaries, and I need to find that dictionary not by its index value but by it containing a 'MarketName': key value.

('\''+(Bittrex_Mrkt_0)['BaseCurrency']+'-'+(Bittrex_Mrkt_0)['MarketCurrency']+'\'')
'BTC-LTC'

That could be inserted after 'MarketName': but I don't know what to do beyond that. I'm still very new to Python. Maybe a for loop is what I'm looking for here?

In other words: I need information from BOTH of those links.

The first link gives the Active True/False and IsRestricted True/False information to the if statements.

Based on that, the Bid and Ask values are going to be pulled next for the same BTC-LTC pair.

So I can't go by the index in the list since they are totally different and will always possibly change.

I have to use 'BTC-LTC' as a way to search the second list of dictionaires, NOT the index.
Reply
#4
Do you want this ?

#!/usr/bin/python3
import sys, pprint, requests

url = "https://api.bittrex.com/api/v1.1/public/getmarkets"
rsp = requests.get(url)
if rsp.status_code == 200:
    markets = rsp.json()["result"]
    #pprint.pprint(markets)
else:
    print("ERROR:", rsp.status_code)
    print(rsp.text)
    sys.exit(1)

url = "https://api.bittrex.com/api/v1.1/public/getmarketsummaries"
rsp = requests.get(url)
if rsp.status_code == 200:
    markets_sum = rsp.json()["result"]
    #pprint.pprint(markets_sum)
else:
    print("ERROR:", rsp.status_code)
    print(rsp.text)
    sys.exit(1)

for market in markets:
    if market['IsActive'] == True  and  market['IsRestricted'] == False:
        ticker = market['BaseCurrency'] + '-' + market['MarketCurrency']
        print(ticker, end="")
        for market_sum in markets_sum:
            if ticker == market_sum['MarketName']:
                print(" found", end="")
                break
        print(".")
#done
Output:
BTC-LTC found BTC-DOGE found. BTC-VTC found. BTC-PPC found. BTC-FTC found. BTC-RDD found. BTC-NXT found. ... ... ...
Reply
#5
I'm really grateful for someone trying to understand that mess of a post I made earlier but I think I was more tired than I realized and just posted a confusing mess lol.

That's not what I am looking for.

I need to search Bittrex_Get_Market_Summaries for 'MarketName': 'BTC-LTC' rather than index 0.

How do I do that?

Also what gets returned should be the entire dictionary containing 'MarketName': 'BTC-LTC'.

So I need to search through all of Bittrex_Get_Market_Summaries for a dictionary containing the key value 'MarketName': 'BTC-LTC' and have that dictionary print or whatever.
Reply
#6
I thought I have done this:

 
for market in markets:
    if market['IsActive'] == True  and  market['IsRestricted'] == False:
        ticker = market['BaseCurrency'] + '-' + market['MarketCurrency']
        print(ticker, end="")
        for market_sum in markets_sum:
            if ticker == market_sum['MarketName']:
                print(" found", end="")
                break
        print(".")
Reply
#7
(May-28-2019, 12:49 PM)heiner55 Wrote: I thought I have done this:

 
for market in markets:
    if market['IsActive'] == True  and  market['IsRestricted'] == False:
        ticker = market['BaseCurrency'] + '-' + market['MarketCurrency']
        print(ticker, end="")
        for market_sum in markets_sum:
            if ticker == market_sum['MarketName']:
                print(" found", end="")
                break
        print(".")

Maybe I just don't understand that code but the output seems different than what I'm needing.

The IsActive and IsRestricted comes from Bittrex_Markets.

So if say BTC-LTC is True and False for those, then I need to search Bittrex_Get_Market_Summaries for a dictionary containing the key value 'MarketName': 'BTC-LTC' to print that entire dictionary.

This next bit is unrelated, but once I have that entire dictionary I will be using the bid and ask from it.

I don't need code just saying if any of them are found or not. If say 'BTC-LTC' is both True and False, I already know it is somewhere in the second variable Bittrex_Get_Market_Summaries.

I think it may be what I need. I think found can be changed to return the dictionary. I'm very new to Python so I apologize for being inept.
Reply
#8
#!/usr/bin/python3
import sys, pprint, requests

url = "https://api.bittrex.com/api/v1.1/public/getmarkets"
rsp = requests.get(url)
if rsp.status_code == 200:
    markets = rsp.json()["result"]
    #pprint.pprint(markets)
else:
    print("ERROR:", rsp.status_code)
    print(rsp.text)
    sys.exit(1)

url = "https://api.bittrex.com/api/v1.1/public/getmarketsummaries"
rsp = requests.get(url)
if rsp.status_code == 200:
    markets_sum = rsp.json()["result"]
    #pprint.pprint(markets_sum)
else:
    print("ERROR:", rsp.status_code)
    print(rsp.text)
    sys.exit(1)

for market in markets:
    if market['IsActive'] == True  and  market['IsRestricted'] == False:
        ticker = market['BaseCurrency'] + '-' + market['MarketCurrency']
        #print(ticker)
        for market_sum in markets_sum:
            if ticker == market_sum['MarketName']:
                print(market_sum)
                break
        print()
#done
Output:
{'MarketName': 'BTC-LTC', 'High': 0.01369299, 'Low': 0.012833, 'Volume': 15403.75712871, 'Last': 0.01303875, 'BaseVolume': 202.53076032, 'TimeStamp': '2019-05-28T13:06:06.417', 'Bid': 0.01305114, 'Ask': 0.01308828, 'OpenBuyOrders': 1357, 'OpenSellOrders': 2551, 'PrevDay': 0.01312497, 'Created': '2014-02-13T00:00:00'} {'MarketName': 'BTC-DOGE', 'High': 3.7e-07, 'Low': 3.5e-07, 'Volume': 144129157.7164047, 'Last': 3.6e-07, 'BaseVolume': 51.59121297, 'TimeStamp': '2019-05-28T13:06:06.417', 'Bid': 3.5e-07, 'Ask': 3.6e-07, 'OpenBuyOrders': 926, 'OpenSellOrders': 7806, 'PrevDay': 3.6e-07, 'Created': '2014-02-13T00:00:00'} {'MarketName': 'BTC-VTC', 'High': 6.227e-05, 'Low': 5.258e-05, 'Volume': 302652.42840458, 'Last': 5.654e-05, 'BaseVolume': 17.38637312, 'TimeStamp': '2019-05-28T13:06:06.417', 'Bid': 5.654e-05, 'Ask': 5.674e-05, 'OpenBuyOrders': 251, 'OpenSellOrders': 4193, 'PrevDay': 5.712e-05, 'Created': '2014-02-13T00:00:00'} ... ... ...
Reply
#9
That's what I've been trying to figure out how to do! Thanks! You're awesome! I will study for loops more now to understand this better.
Reply
#10
Super. Here the latest code:

#!/usr/bin/python3
import sys, pprint, requests

# get data1
url = "https://api.bittrex.com/api/v1.1/public/getmarkets"
rsp = requests.get(url)
if rsp.status_code == 200:
    markets = rsp.json()["result"]
    #pprint.pprint(markets)
else:
    print("ERROR:", rsp.status_code)
    print(rsp.text)
    sys.exit(1)

# get data2
url = "https://api.bittrex.com/api/v1.1/public/getmarketsummaries"
rsp = requests.get(url)
if rsp.status_code == 200:
    marketsummaries = rsp.json()["result"]
    #pprint.pprint(marketsummaries)
else:
    print("ERROR:", rsp.status_code)
    print(rsp.text)
    sys.exit(1)

# find all tickers which are active and not restricted
list_found = [ ]
for market in markets:
    if market['IsActive'] == True  and  market['IsRestricted'] == False:
        ticker = market['BaseCurrency'] + '-' + market['MarketCurrency']
        for marketsummary in marketsummaries:
            if ticker == marketsummary['MarketName']:
                list_found += [ marketsummary ]
                break

# do something with list
pprint.pprint(list_found)

#done
Output:
[{'Ask': 0.01307497, 'BaseVolume': 201.9121671, 'Bid': 0.0130465, 'Created': '2014-02-13T00:00:00', 'High': 0.01369299, 'Last': 0.01307505, 'Low': 0.012833, 'MarketName': 'BTC-LTC', 'OpenBuyOrders': 1354, 'OpenSellOrders': 2553, 'PrevDay': 0.01307419, 'TimeStamp': '2019-05-28T13:22:21.84', 'Volume': 15356.44572863}, {'Ask': 3.6e-07, 'BaseVolume': 51.37509171, 'Bid': 3.5e-07, 'Created': '2014-02-13T00:00:00', 'High': 3.7e-07, 'Last': 3.5e-07, 'Low': 3.5e-07, 'MarketName': 'BTC-DOGE', 'OpenBuyOrders': 933, 'OpenSellOrders': 7804, 'PrevDay': 3.6e-07, 'TimeStamp': '2019-05-28T13:22:21.84', 'Volume': 143504743.7629786}, ... ... ...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,641 Jan-05-2024, 08:30 PM
Last Post: sgrey
  Dictionary in a list bashage 2 544 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 667 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Search Excel File with a list of values huzzug 4 1,218 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Sort a list of dictionaries by the only dictionary key Calab 1 488 Oct-27-2023, 03:03 PM
Last Post: buran
  Access list of dictionaries britesc 4 1,069 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  How to add list to dictionary? Kull_Khan 3 998 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  search a list or tuple for a specific type ot class Skaperen 8 1,920 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  check if element is in a list in a dictionary value ambrozote 4 1,964 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,959 Apr-28-2022, 06:59 AM
Last Post: buran

Forum Jump:

User Panel Messages

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