Khalil Mebarkia
Khalil Mebarkia

Reputation: 159

How to get element of a list in python

I have the following the function:

price = client.futures_recent_trades(symbol="BTCUSDT", limit=1)

of type <class 'list'> that has output:

[{'id': 1644406868, 'price': '58024.69', 'qty': '0.009', 'quoteQty': '522.22', 'time': 1637262411652, 'isBuyerMaker': False}]

and:

I want to return price value only : 'price': '58024.69' and therefore,

print(len(price))=1 but print(len(price[0]))= 6

But when I tried price[0][1], it throws an error list index out of range

Upvotes: 1

Views: 102

Answers (3)

KSVelArc
KSVelArc

Reputation: 88

>>> list(price[0].items())[1]
('price', '58024.69')

Upvotes: 2

user3934647
user3934647

Reputation:

thisdict = dict(price)

x = thisdict.get("price")

print("price: " + x)

Upvotes: 0

Alex Rajan Samuel
Alex Rajan Samuel

Reputation: 144

It's a dictionary within a list, so you should pass the key for the second index, like shown below.

price[0]['price']

Upvotes: 3

Related Questions