Home > Net >  Extracting values containing certain string froma list of dictionaries
Extracting values containing certain string froma list of dictionaries

Time:02-07

I have the followning list of dictionaries:

[{'symbol': 'ETHBTC', 'price': '0.07216700'}, {'symbol': 'LTCBTC', 'price': '0.00299400'}, {'symbol': 'BNBBTC', 'price': '0.00999600'}, {'symbol': 'NEOBTC', 'price': '0.00054300'}, {'symbol': 'QTUMETH', 'price': '0.00238900'}, {'symbol': 'EOSETH', 'price': '0.00084200'}, {'symbol': 'SNTETH', 'price': '0.00002096'}, {'symbol': 'BNTETH', 'price': '0.00089400'}, {'symbol': 'BCCBTC', 'price': '0.07908100'}, {'symbol': 'GASBTC', 'price': '0.00012830'}, {'symbol': 'BNBETH', 'price': '0.13850000'}, {'symbol': 'BTCUSDT', 'price': '42580.58000000'}, {'symbol': 'ETHUSDT', 'price': '3072.66000000'}, {'symbol': 'HSRBTC', 'price': '0.00041400'}, {'symbol': 'OAXETH', 'price': '0.00017780'}, {'symbol': 'DNTETH', 'price': '0.00002801'}, {'symbol': 'MCOETH', 'price': '0.00577200'}, {'symbol': 'ICNETH', 'price': '0.00166300'}, {'symbol': 'MCOBTC', 'price': '0.00021140'}]

How can I extract from it all strings having 'BTC' as a part of them? Also can I filter them while extracting to drop some predetermined?

Thanks

CodePudding user response:

A list comprehension seems to work:

lst = [{'symbol': 'ETHBTC', 'price': '0.07216700'}, {'symbol': 'LTCBTC', 'price': '0.00299400'}, {'symbol': 'BNBBTC', 'price': '0.00999600'}, {'symbol': 'NEOBTC', 'price': '0.00054300'}, {'symbol': 'QTUMETH', 'price': '0.00238900'}, {'symbol': 'EOSETH', 'price': '0.00084200'}, {'symbol': 'SNTETH', 'price': '0.00002096'}, {'symbol': 'BNTETH', 'price': '0.00089400'}, {'symbol': 'BCCBTC', 'price': '0.07908100'}, {'symbol': 'GASBTC', 'price': '0.00012830'}, {'symbol': 'BNBETH', 'price': '0.13850000'}, {'symbol': 'BTCUSDT', 'price': '42580.58000000'}, {'symbol': 'ETHUSDT', 'price': '3072.66000000'}, {'symbol': 'HSRBTC', 'price': '0.00041400'}, {'symbol': 'OAXETH', 'price': '0.00017780'}, {'symbol': 'DNTETH', 'price': '0.00002801'}, {'symbol': 'MCOETH', 'price': '0.00577200'}, {'symbol': 'ICNETH', 'price': '0.00166300'}, {'symbol': 'MCOBTC', 'price': '0.00021140'}]
out = [d for d in lst if 'BTC' in d['symbol']]
print(out)

Output:

[{'symbol': 'ETHBTC', 'price': '0.07216700'},
 {'symbol': 'LTCBTC', 'price': '0.00299400'},
 {'symbol': 'BNBBTC', 'price': '0.00999600'},
 {'symbol': 'NEOBTC', 'price': '0.00054300'},
 {'symbol': 'BCCBTC', 'price': '0.07908100'},
 {'symbol': 'GASBTC', 'price': '0.00012830'},
 {'symbol': 'BTCUSDT', 'price': '42580.58000000'},
 {'symbol': 'HSRBTC', 'price': '0.00041400'},
 {'symbol': 'MCOBTC', 'price': '0.00021140'}]

CodePudding user response:

You can also use a generator

list = [{'symbol': 'ETHBTC', 'price': '0.07216700'}, {'symbol': 'LTCBTC', 'price': '0.00299400'}, {'symbol': 'BNBBTC', 'price': '0.00999600'}, {'symbol': 'NEOBTC', 'price': '0.00054300'}, {'symbol': 'QTUMETH', 'price': '0.00238900'}, {'symbol': 'EOSETH', 'price': '0.00084200'}, {'symbol': 'SNTETH', 'price': '0.00002096'}, {'symbol': 'BNTETH', 'price': '0.00089400'}, {'symbol': 'BCCBTC', 'price': '0.07908100'}, {'symbol': 'GASBTC', 'price': '0.00012830'}, {'symbol': 'BNBETH', 'price': '0.13850000'}, {'symbol': 'BTCUSDT', 'price': '42580.58000000'}, {'symbol': 'ETHUSDT', 'price': '3072.66000000'}, {'symbol': 'HSRBTC', 'price': '0.00041400'}, {'symbol': 'OAXETH', 'price': '0.00017780'}, {'symbol': 'DNTETH', 'price': '0.00002801'}, {'symbol': 'MCOETH', 'price': '0.00577200'}, {'symbol': 'ICNETH', 'price': '0.00166300'}, {'symbol': 'MCOBTC', 'price': '0.00021140'}]
try:
    output = (x for x in list if 'BTC' in x['symbol'])
    for i in output:
        print(i)
except StopIteration:
    pass
  •  Tags:  
  • Related