Python Forum

Full Version: Pulling Information Out of Dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello there!

Please forgive me if this is a basic question...

Long story short, I'm trying to pull out this specific number value (https://photos.app.goo.gl/ShEMQWR4utM6LXXu8) from a function I call. I will paste the text below for reference.

I can actually get it doing this code here, but I am afraid that the key will change, so I'm looking for a more permanent way of getting it.
positions = r.get_crypto_positions(info="quantity")
print(positions[4])
How can I make this where I can always pull out this number even if new items are added to the list later? I'm not sure how to specify the key another way.

Many thanks in advance for your help!

Kind regards,
-Griever

For reference, here's the text of the output without any parameters (the result from the photo I shared above).
[{'account_id': '6b8ed042-7c61-4c4c-bae6-4a35276a35a4', 'cost_bases': [{'currency_id': '1072fc76-1862-41ab-82c2-485837590762', 'direct_cost_basis': '0.000000000000000000', 'direct_quantity': '0.000000000000000000', 'id': 'a6d61720-3591-44c4-9e3c-1eded3ea8a1b', 'intraday_quantity': '0.000000000000000000', 'intraday_cost_basis': '0.000000000000000000', 'marked_cost_basis': '0.000000000000000000', 'marked_quantity': '0.000000000000000000'}], 'created_at': '2020-07-29T09:00:30.447042-04:00', 'currency': {'brand_color': '99C061', 'code': 'BCH', 'id': '913a38ed-36f3-45fb-a967-fb6e30d4a7fb', 'increment': '0.000000010000000000', 'name': 'Bitcoin Cash', 'type': 'cryptocurrency'}, 'id': '3b563fab-a6de-45b8-b998-cc569da6f9f3', 'quantity': '0.000000000000000000', 'quantity_available': '0.000000000000000000', 'quantity_held_for_buy': '0.000000000000000000', 'quantity_held_for_sell': '0.000000000000000000', 'updated_at': '2020-07-29T11:22:55.694728-04:00'}, {'account_id': '6b8ed042-7c61-4c4c-bae6-4a35276a35a4', 'cost_bases': [{'currency_id': '1072fc76-1862-41ab-82c2-485837590762', 'direct_cost_basis': '0.000000000000000000', 'direct_quantity': '0.000000000000000000', 'id': '66054860-1f6e-410e-bd54-1246b5f878c4', 'intraday_quantity': '0.000000000000000000', 'intraday_cost_basis': '0.000000000000000000', 'marked_cost_basis': '0.000000000000000000', 'marked_quantity': '0.000000000000000000'}], 'created_at': '2020-07-29T07:57:56.945681-04:00', 'currency': {'brand_color': 'EA963D', 'code': 'BTC', 'id': 'd674efea-e623-4396-9026-39574b92b093', 'increment': '0.000000010000000000', 'name': 'Bitcoin', 'type': 'cryptocurrency'}, 'id': 'a9d18951-b48e-4675-8e46-bea05e130c88', 'quantity': '0.000000000000000000', 'quantity_available': '0.000000000000000000', 'quantity_held_for_buy': '0.000000000000000000', 'quantity_held_for_sell': '0.000000000000000000', 'updated_at': '2020-08-02T10:47:13.450915-04:00'}, {'account_id': '6b8ed042-7c61-4c4c-bae6-4a35276a35a4', 'cost_bases': [{'currency_id': '1072fc76-1862-41ab-82c2-485837590762', 'direct_cost_basis': '0.000000000000000000', 'direct_quantity': '0.000000000000000000', 'id': '428a85b5-d451-4f51-9d79-6810992629d9', 'intraday_quantity': '0.000000000000000000', 'intraday_cost_basis': '0.000000000000000000', 'marked_cost_basis': '0.000000000000000000', 'marked_quantity': '0.000000000000000000'}], 'created_at': '2020-07-29T07:48:51.551755-04:00', 'currency': {'brand_color': '', 'code': 'BSV', 'id': '4a7924c8-d554-47d6-8595-c4e522b0183a', 'increment': '0.000000010000000000', 'name': 'Bitcoin SV', 'type': 'cryptocurrency'}, 'id': '91d678bb-eb3a-4a55-9a43-509e38f8933b', 'quantity': '0.000000000000000000', 'quantity_available': '0.000000000000000000', 'quantity_held_for_buy': '0.000000000000000000', 'quantity_held_for_sell': '0.000000000000000000', 'updated_at': '2020-07-29T11:24:37.542736-04:00'}, {'account_id': '6b8ed042-7c61-4c4c-bae6-4a35276a35a4', 'cost_bases': [{'currency_id': '1072fc76-1862-41ab-82c2-485837590762', 'direct_cost_basis': '0.000000000000000000', 'direct_quantity': '0.000000000000000000', 'id': '781d196d-d437-4d4d-8058-4a0186a13314', 'intraday_quantity': '0.000000000000000000', 'intraday_cost_basis': '0.000000000000000000', 'marked_cost_basis': '0.000000000000000000', 'marked_quantity': '0.000000000000000000'}], 'created_at': '2020-07-29T07:31:52.911006-04:00', 'currency': {'brand_color': '', 'code': 'ETC', 'id': 'ee3bcf3e-4ac7-4f0a-b887-4cea2b49ff70', 'increment': '0.000001000000000000', 'name': 'Ethereum Classic', 'type': 'cryptocurrency'}, 'id': 'ff72e036-fb81-438a-ad2f-19e4f3fa8652', 'quantity': '0.000000000000000000', 'quantity_available': '0.000000000000000000', 'quantity_held_for_buy': '0.000000000000000000', 'quantity_held_for_sell': '0.000000000000000000', 'updated_at': '2020-07-29T11:06:45.166313-04:00'}, {'account_id': '6b8ed042-7c61-4c4c-bae6-4a35276a35a4', 'cost_bases': [{'currency_id': '1072fc76-1862-41ab-82c2-485837590762', 'direct_cost_basis': '186.510000000000000000', 'direct_quantity': '0.470445000000000000', 'id': 'a346e0b4-d4cc-4076-953c-7901d5aa61f6', 'intraday_quantity': '0.000000000000000000', 'intraday_cost_basis': '0.000000000000000000', 'marked_cost_basis': '0.000000000000000000', 'marked_quantity': '0.000000000000000000'}], 'created_at': '2020-07-28T20:21:19.411321-04:00', 'currency': {'brand_color': '707DB5', 'code': 'ETH', 'id': 'c527c04a-394b-4a44-ae07-19b901ca609c', 'increment': '0.000000000000000001', 'name': 'Ethereum', 'type': 'cryptocurrency'}, 'id': '9b00624f-a093-4ae4-9842-8f7cee78ff5e', 'quantity': '0.470445000000000000', 'quantity_available': '0.470445000000000000', 'quantity_held_for_buy': '0.000000000000000000', 'quantity_held_for_sell': '0.000000000000000000', 'updated_at': '2020-08-06T13:46:29.049903-04:00'}, {'account_id': '6b8ed042-7c61-4c4c-bae6-4a35276a35a4', 'cost_bases': [{'currency_id': '1072fc76-1862-41ab-82c2-485837590762', 'direct_cost_basis': '0.000000000000000000', 'direct_quantity': '0.000000000000000000', 'id': '17122f78-0db7-4838-a099-bce0a62ab01d', 'intraday_quantity': '0.000000000000000000', 'intraday_cost_basis': '0.000000000000000000', 'marked_cost_basis': '0.000000000000000000', 'marked_quantity': '0.000000000000000000'}], 'created_at': '2020-07-28T20:16:19.425045-04:00', 'currency': {'brand_color': 'BEBBBB', 'code': 'LTC', 'id': 'f9432751-b54d-4d84-b573-f06dc390b766', 'increment': '0.000000010000000000', 'name': 'Litecoin', 'type': 'cryptocurrency'}, 'id': 'afb1ae30-79f1-4d9a-97ad-3b34b1c678ae', 'quantity': '0.000000000000000000', 'quantity_available': '0.000000000000000000', 'quantity_held_for_buy': '0.000000000000000000', 'quantity_held_for_sell': '0.000000000000000000', 'updated_at': '2020-07-29T11:30:47.752134-04:00'}]
What makes that the number you want compared to the other "quantity" values there? Is it particular account? How can the program identify the correct one?
(Aug-12-2020, 02:20 AM)bowlofred Wrote: [ -> ]What makes that the number you want compared to the other "quantity" values there? Is it particular account? How can the program identify the correct one?

Hi bowlofred!

Many thanks for your response!

And yeah, the other quantity amounts are for separate accounts. I think it's essentially a list inside a list (and maybe inside another list), but I'm not sure how to pull this out better. Here's a zoomed in section of the data I want to focus on. To recap, I just want to be able to pull out the quantity value of 0.470445000000000000.
{'brand_color': '707DB5', 'code': 'ETH', 'id': 'c527c04a-394b-4a44-ae07-19b901ca609c', 'increment': '0.000000000000000001', 'name': 'Ethereum', 'type': 'cryptocurrency'}, 'id': '9b00624f-a093-4ae4-9842-8f7cee78ff5e', 'quantity': '0.470445000000000000', 'quantity_available': '0.470445000000000000', 'quantity_held_for_buy': '0.000000000000000000', 'quantity_held_for_sell': '0.000000000000000000', 'updated_at': '2020-08-06T13:46:29.049903-04:00'}
Many thanks again for all of your support!

Kind regards,
-Griever
probably, if you format the json properly (because your data obviously come from JSON response):
Output:
[ { "account_id": "6b8ed042-7c61-4c4c-bae6-4a35276a35a4", "cost_bases": [ { "currency_id": "1072fc76-1862-41ab-82c2-485837590762", "direct_cost_basis": "0.000000000000000000", "direct_quantity": "0.000000000000000000", "id": "a6d61720-3591-44c4-9e3c-1eded3ea8a1b", "intraday_quantity": "0.000000000000000000", "intraday_cost_basis": "0.000000000000000000", "marked_cost_basis": "0.000000000000000000", "marked_quantity": "0.000000000000000000" } ], "created_at": "2020-07-29T09:00:30.447042-04:00", "currency": { "brand_color": "99C061", "code": "BCH", "id": "913a38ed-36f3-45fb-a967-fb6e30d4a7fb", "increment": "0.000000010000000000", "name": "Bitcoin Cash", "type": "cryptocurrency" }, "id": "3b563fab-a6de-45b8-b998-cc569da6f9f3", "quantity": "0.000000000000000000", "quantity_available": "0.000000000000000000", "quantity_held_for_buy": "0.000000000000000000", "quantity_held_for_sell": "0.000000000000000000", "updated_at": "2020-07-29T11:22:55.694728-04:00" }, { "account_id": "6b8ed042-7c61-4c4c-bae6-4a35276a35a4", "cost_bases": [ { "currency_id": "1072fc76-1862-41ab-82c2-485837590762", "direct_cost_basis": "0.000000000000000000", "direct_quantity": "0.000000000000000000", "id": "66054860-1f6e-410e-bd54-1246b5f878c4", "intraday_quantity": "0.000000000000000000", "intraday_cost_basis": "0.000000000000000000", "marked_cost_basis": "0.000000000000000000", "marked_quantity": "0.000000000000000000" } ], "created_at": "2020-07-29T07:57:56.945681-04:00", "currency": { "brand_color": "EA963D", "code": "BTC", "id": "d674efea-e623-4396-9026-39574b92b093", "increment": "0.000000010000000000", "name": "Bitcoin", "type": "cryptocurrency" }, "id": "a9d18951-b48e-4675-8e46-bea05e130c88", "quantity": "0.000000000000000000", "quantity_available": "0.000000000000000000", "quantity_held_for_buy": "0.000000000000000000", "quantity_held_for_sell": "0.000000000000000000", "updated_at": "2020-08-02T10:47:13.450915-04:00" }, { "account_id": "6b8ed042-7c61-4c4c-bae6-4a35276a35a4", "cost_bases": [ { "currency_id": "1072fc76-1862-41ab-82c2-485837590762", "direct_cost_basis": "0.000000000000000000", "direct_quantity": "0.000000000000000000", "id": "428a85b5-d451-4f51-9d79-6810992629d9", "intraday_quantity": "0.000000000000000000", "intraday_cost_basis": "0.000000000000000000", "marked_cost_basis": "0.000000000000000000", "marked_quantity": "0.000000000000000000" } ], "created_at": "2020-07-29T07:48:51.551755-04:00", "currency": { "brand_color": "", "code": "BSV", "id": "4a7924c8-d554-47d6-8595-c4e522b0183a", "increment": "0.000000010000000000", "name": "Bitcoin SV", "type": "cryptocurrency" }, "id": "91d678bb-eb3a-4a55-9a43-509e38f8933b", "quantity": "0.000000000000000000", "quantity_available": "0.000000000000000000", "quantity_held_for_buy": "0.000000000000000000", "quantity_held_for_sell": "0.000000000000000000", "updated_at": "2020-07-29T11:24:37.542736-04:00" }, { "account_id": "6b8ed042-7c61-4c4c-bae6-4a35276a35a4", "cost_bases": [ { "currency_id": "1072fc76-1862-41ab-82c2-485837590762", "direct_cost_basis": "0.000000000000000000", "direct_quantity": "0.000000000000000000", "id": "781d196d-d437-4d4d-8058-4a0186a13314", "intraday_quantity": "0.000000000000000000", "intraday_cost_basis": "0.000000000000000000", "marked_cost_basis": "0.000000000000000000", "marked_quantity": "0.000000000000000000" } ], "created_at": "2020-07-29T07:31:52.911006-04:00", "currency": { "brand_color": "", "code": "ETC", "id": "ee3bcf3e-4ac7-4f0a-b887-4cea2b49ff70", "increment": "0.000001000000000000", "name": "Ethereum Classic", "type": "cryptocurrency" }, "id": "ff72e036-fb81-438a-ad2f-19e4f3fa8652", "quantity": "0.000000000000000000", "quantity_available": "0.000000000000000000", "quantity_held_for_buy": "0.000000000000000000", "quantity_held_for_sell": "0.000000000000000000", "updated_at": "2020-07-29T11:06:45.166313-04:00" }, { "account_id": "6b8ed042-7c61-4c4c-bae6-4a35276a35a4", "cost_bases": [ { "currency_id": "1072fc76-1862-41ab-82c2-485837590762", "direct_cost_basis": "186.510000000000000000", "direct_quantity": "0.470445000000000000", "id": "a346e0b4-d4cc-4076-953c-7901d5aa61f6", "intraday_quantity": "0.000000000000000000", "intraday_cost_basis": "0.000000000000000000", "marked_cost_basis": "0.000000000000000000", "marked_quantity": "0.000000000000000000" } ], "created_at": "2020-07-28T20:21:19.411321-04:00", "currency": { "brand_color": "707DB5", "code": "ETH", "id": "c527c04a-394b-4a44-ae07-19b901ca609c", "increment": "0.000000000000000001", "name": "Ethereum", "type": "cryptocurrency" }, "id": "9b00624f-a093-4ae4-9842-8f7cee78ff5e", "quantity": "0.470445000000000000", "quantity_available": "0.470445000000000000", "quantity_held_for_buy": "0.000000000000000000", "quantity_held_for_sell": "0.000000000000000000", "updated_at": "2020-08-06T13:46:29.049903-04:00" }, { "account_id": "6b8ed042-7c61-4c4c-bae6-4a35276a35a4", "cost_bases": [ { "currency_id": "1072fc76-1862-41ab-82c2-485837590762", "direct_cost_basis": "0.000000000000000000", "direct_quantity": "0.000000000000000000", "id": "17122f78-0db7-4838-a099-bce0a62ab01d", "intraday_quantity": "0.000000000000000000", "intraday_cost_basis": "0.000000000000000000", "marked_cost_basis": "0.000000000000000000", "marked_quantity": "0.000000000000000000" } ], "created_at": "2020-07-28T20:16:19.425045-04:00", "currency": { "brand_color": "BEBBBB", "code": "LTC", "id": "f9432751-b54d-4d84-b573-f06dc390b766", "increment": "0.000000010000000000", "name": "Litecoin", "type": "cryptocurrency" }, "id": "afb1ae30-79f1-4d9a-97ad-3b34b1c678ae", "quantity": "0.000000000000000000", "quantity_available": "0.000000000000000000", "quantity_held_for_buy": "0.000000000000000000", "quantity_held_for_sell": "0.000000000000000000", "updated_at": "2020-07-29T11:30:47.752134-04:00" } ]
you will be able to see what complex structure you have

obviously its multiple records for same account_id.

for item in data:
    print(f"{item['currency']['code']} --> {item['quantity']}")
Output:
BCH --> 0.000000000000000000 BTC --> 0.000000000000000000 BSV --> 0.000000000000000000 ETC --> 0.000000000000000000 ETH --> 0.470445000000000000 LTC --> 0.000000000000000000
Hi buran!

This is very helpful! I'm going to try it today and will let you know how I get along.

Seeing the output straightened out and organized does make it much easier to read!

Since I only want to really pull information out of the ETH code, I imagine that I'll need to make a make my own array with the output from your loop and essentially make my own key names so that I can pull out ETH consistently every time. Is that the way you would go about it? Sorry if I'm not making any sense! I'm still very much a beginner here!

Thanks again so much for your help and support!

Kind regards,
-Griever