Python Forum
creating a list of dictionaries from API calls
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating a list of dictionaries from API calls
#1
Hello,

I am having trouble formulating a list of dictionaries which incorporate information from an API call which will set the KEY:VALUE pairs.

I am trying to get this result:

Output:
MainList[ {'Asset_ID': 012334, 'AAPL': 238.75}, {'Asset_ID': 567890, 'FB': 28.50}, {'Asset_ID': 123345, 'BT': 203.55}, {'Asset_ID': 678901, 'C': 38.25} etc.............]
and this is my current code:

import requests
import json

BASE_URL = 'https://paper-api.alpaca.markets'
HEADERS = {'APCA-API-KEY-ID': "", 'APCA-API-SECRET-KEY': ""}
ACCOUNT_URL = '{}/v2/account'.format(BASE_URL)
ASSETS_URL = '{}/v2/assets'.format(BASE_URL)
POSITIONS_URL = '{}/v2/positions'.format(BASE_URL)

#asset API provides the stock symbol(AAPL,FB etc.)& the associated asset ID's.
assets_r = requests.get(ASSETS_URL, headers=HEADERS)
get_assets = json.loads(assets_r.content)

#positions API provides the Current_value
positions_r = requests.get(POSITIONS_URL, headers=HEADERS)
get_position = json.loads(positions_r.content)

dictionary = dict

Symbol_items = dictionary(get_assets)['symbol', range(1)]

Asset_ID_items = dictionary(get_assets)['id', range(1)]

price_items = dictionary(get_position)['current_price', range(1)]

dictionary = {'Asset_ID' : Asset_ID_items, Symbol_items: price_items}

print(dictionary)
--- when it runs I get this message,

Error:
Traceback (most recent call last): File "C:/Users/engli/PycharmProjects/tester01/rambo/first_Test.py", line 24, in <module> Symbol_items = dictionary(get_assets)['symbol', range(1)] ValueError: dictionary update sequence element #0 has length 10; 2 is required
Any help will be greatly appreciated!

Kind Regards,

Andrew
Reply


Messages In This Thread
creating a list of dictionaries from API calls - by AndrewEnglsh101 - Apr-02-2020, 08:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 2 628 Apr-29-2024, 04:38 PM
Last Post: Calab
  Access list of dictionaries britesc 4 1,110 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,947 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  function that returns a list of dictionaries nostradamus64 2 1,785 May-06-2021, 09:58 PM
Last Post: nostradamus64
  Calls to Attributes of a Class SKarimi 3 3,432 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  convert List with dictionaries to a single dictionary iamaghost 3 2,889 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  Creating list of lists from generator object t4keheart 1 2,229 Nov-13-2020, 04:59 AM
Last Post: perfringo
  Creating a dictionary from a list Inkanus 5 3,198 Nov-06-2020, 06:11 PM
Last Post: DeaD_EyE
  list call problem in generator function using iteration and recursive calls postta 1 1,940 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,325 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138

Forum Jump:

User Panel Messages

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