Reputation: 542
I have a nested list and I need to filter out a key / value based on a condition.
the list:
[{'buyer': False,
'commission': '0.01333920',
'commissionAsset': 'USDT',
'id': 52549382,
'maker': False,
'marginAsset': 'USDT',
'orderId': 2448678953,
'positionSide': 'BOTH',
'price': '3334.80',
'qty': '0.010',
'quoteQty': '33.34800',
'realizedPnl': '0',
'side': 'SELL',
'symbol': 'MKRUSDT',
'time': 1628311810877},
{'buyer': True,
'commission': '0.01334440',
'commissionAsset': 'USDT',
'id': 52544760,
'maker': False,
'marginAsset': 'USDT',
'orderId': 2447909783,
'positionSide': 'BOTH',
'price': '3336.10',
'qty': '0.010',
'quoteQty': '33.36100',
'realizedPnl': '-0.29400000',
'side': 'BUY',
'symbol': 'MKRUSDT',
'time': 1628309268672},
{'buyer': False,
'commission': '0.01322680',
'commissionAsset': 'USDT',
'id': 52532126,
'maker': False,
'marginAsset': 'USDT',
'orderId': 2447909485,
'positionSide': 'BOTH',
'price': '3306.70',
'qty': '0.010',
'quoteQty': '33.06700',
'realizedPnl': '0',
'side': 'SELL',
'symbol': 'MKRUSDT',
'time': 1628301912771},
{'buyer': True,
'commission': '0.01319760',
'commissionAsset': 'USDT',
'id': 52525468,
'maker': False,
'marginAsset': 'USDT',
'orderId': 2447502727,
'positionSide': 'BOTH',
'price': '3299.40',
'qty': '0.010',
'quoteQty': '32.99400',
'realizedPnl': '-0.29700000',
'side': 'BUY',
'symbol': 'MKRUSDT',
'time': 1628297857042},
Now, how can I get the 'time' value of the nested block where the 'id' is equal to 52532126 ? Although I prefer vectorization I tried to iterate through the list but I had no success.
for item in response_tradelist:
if item == '52532126':
print('ok')
Even If it would work how do I get the 'time' value in the corresponding block?
Upvotes: 0
Views: 826
Reputation: 1
You need to iterate thru the list of dictionaries and check the i["time"] index for each one.
dictList = [{
'buyer': False,
'commission': '0.01333920',
'commissionAsset': 'USDT',
'id': 52549382,
'maker': False,
'marginAsset': 'USDT',
'orderId': 2448678953,
'positionSide': 'BOTH',
'price': '3334.80',
'qty': '0.010',
'quoteQty': '33.34800',
'realizedPnl': '0',
'side': 'SELL',
'symbol': 'MKRUSDT',
'time': 1628311810877
},
{
'buyer': True,
'commission': '0.01334440',
'commissionAsset': 'USDT',
'id': 52544760,
'maker': False,
'marginAsset': 'USDT',
'orderId': 2447909783,
'positionSide': 'BOTH',
'price': '3336.10',
'qty': '0.010',
'quoteQty': '33.36100',
'realizedPnl': '-0.29400000',
'side': 'BUY',
'symbol': 'MKRUSDT',
'time': 1628309268672
},
{
'buyer': False,
'commission': '0.01322680',
'commissionAsset': 'USDT',
'id': 52532126,
'maker': False,
'marginAsset': 'USDT',
'orderId': 2447909485,
'positionSide': 'BOTH',
'price': '3306.70',
'qty': '0.010',
'quoteQty': '33.06700',
'realizedPnl': '0',
'side': 'SELL',
'symbol': 'MKRUSDT',
'time': 1628301912771},
{
'buyer': True,
'commission': '0.01319760',
'commissionAsset': 'USDT',
'id': 52525468,
'maker': False,
'marginAsset': 'USDT',
'orderId': 2447502727,
'positionSide': 'BOTH',
'price': '3299.40',
'qty': '0.010',
'quoteQty': '32.99400',
'realizedPnl': '-0.29700000',
'side': 'BUY',
'symbol': 'MKRUSDT',
'time': 1628297857042
}]
searchValue = 1628297857042
for i in dictList:
if i["time"] == searchValue:
print(f"Found: {i["time"]}")
Alternatively if you were looking to do some "fun" python magic -
value = [True for item in dictList if item["time"] == searchValue]
print(value)
You could use list comprehension, unfortunately it does return an array, but that could be fixed pretty easily.
Upvotes: 0
Reputation: 72
Since the value of your id is in integer in that block of dictionary, thus you cannot check with string value, you need to check value like item['id']== 52532126
l=[{'buyer': False,
'commission': '0.01333920',
'commissionAsset': 'USDT',
'id': 52549382,
'maker': False,
'marginAsset': 'USDT',
'orderId': 2448678953,
'positionSide': 'BOTH',
'price': '3334.80',
'qty': '0.010',
'quoteQty': '33.34800',
'realizedPnl': '0',
'side': 'SELL',
'symbol': 'MKRUSDT',
'time': 1628311810877},
{'buyer': True,
'commission': '0.01334440',
'commissionAsset': 'USDT',
'id': 52544760,
'maker': False,
'marginAsset': 'USDT',
'orderId': 2447909783,
'positionSide': 'BOTH',
'price': '3336.10',
'qty': '0.010',
'quoteQty': '33.36100',
'realizedPnl': '-0.29400000',
'side': 'BUY',
'symbol': 'MKRUSDT',
'time': 1628309268672},
{'buyer': False,
'commission': '0.01322680',
'commissionAsset': 'USDT',
'id': 52532126,
'maker': False,
'marginAsset': 'USDT',
'orderId': 2447909485,
'positionSide': 'BOTH',
'price': '3306.70',
'qty': '0.010',
'quoteQty': '33.06700',
'realizedPnl': '0',
'side': 'SELL',
'symbol': 'MKRUSDT',
'time': 1628301912771},
{'buyer': True,
'commission': '0.01319760',
'commissionAsset': 'USDT',
'id': 52525468,
'maker': False,
'marginAsset': 'USDT',
'orderId': 2447502727,
'positionSide': 'BOTH',
'price': '3299.40',
'qty': '0.010',
'quoteQty': '32.99400',
'realizedPnl': '-0.29700000',
'side': 'BUY',
'symbol': 'MKRUSDT',
'time': 1628297857042}]
for d in l:
if d["id"]==52532126:
print(d["time"])
and if you want to print the whole dictionary then ,
for d in l:
if d["id"]==52532126:
print(d)
Upvotes: 1
Reputation: 299
Since it's a dict
for item in response_tradelist:
if item['id] == '52532126':
print(item['time'])
Upvotes: 0
Reputation: 4779
That's a list of dictionaries, you must compare with id
in each item and then print the time
if id
is a match.
Also id
value is not a string, so don't use quotes around id
.
Try this:
for item in response_tradelist:
if item['id'] == 52532126:
print(item['time'])
Upvotes: 3
Reputation: 975
You are halfway there. You only need to access the right element instead of just item
and the ID value is an int
not an str
Fixing those two things gives you the result:
for item in response_tradelist:
if item['id'] == 52532126:
print('ok')
you can filter using a filter
function in case you have multiple values and want to retrieve a list.
filtered_list = list(filter(lambda x: x['id'] == 52525468, response_tradelist))
reference: Filtering a list of strings based on contents
Upvotes: 0
Reputation: 12731
if you want to print the dictionary with id 52532126:
for dic in response_tradelist:
if dic['id'] == 52532126:
print(dic)
if you want time specifically, replace print(dic)
with:
print(dic['time'])
Upvotes: 1