How to convert a single item list to a dictionary?

Question:

symbol = 'AAPL'
api_url = f'https://financialmodelingprep.com/api/v3/profile/{symbol}?apikey={FMP_API_TOKEN}'
data = requests.get(api_url).json()
data

I called this api in a Jupyter notebook and the json I received was a single item list that looked like the following:

[{'symbol': 'AAPL',
  'price': 155.35,
  'beta': 1.19455,
  'volAvg': 93789221,
  'mktCap': 2514370953216,
  'lastDiv': 0.89,
  'range': '129.04-182.94',
  'changes': 2.3100128,
  'companyName': 'Apple Inc.',
  'currency': 'USD',
  'cik': '0000320193',
  'isin': 'US0378331005',
  'cusip': '037833100',
  'exchange': 'NASDAQ Global Select',
  'exchangeShortName': 'NASDAQ',
  'industry': 'Consumer Electronics',
  'website': 'https://www.apple.com',
  'description': 'Apple Inc. designs, manufactures, and markets smartphones, personal computers, tablets, wearables, and accessories worldwide. It also sells various related services. In addition, the company offers iPhone, a line of smartphones; Mac, a line of personal computers; iPad, a line of multi-purpose tablets; AirPods Max, an over-ear wireless headphone; and wearables, home, and accessories comprising AirPods, Apple TV, Apple Watch, Beats products, HomePod, and iPod touch. Further, it provides AppleCare support services; cloud services store services; and operates various platforms, including the App Store that allow customers to discover and download applications and digital content, such as books, music, video, games, and podcasts. Additionally, the company offers various services, such as Apple Arcade, a game subscription service; Apple Music, which offers users a curated listening experience with on-demand radio stations; Apple News+, a subscription news and magazine service; Apple TV+, which offers exclusive original content; Apple Card, a co-branded credit card; and Apple Pay, a cashless payment service, as well as licenses its intellectual property. The company serves consumers, and small and mid-sized businesses; and the education, enterprise, and government markets. It distributes third-party applications for its products through the App Store. The company also sells its products through its retail and online stores, and direct sales force; and third-party cellular network carriers, wholesalers, retailers, and resellers. Apple Inc. was incorporated in 1977 and is headquartered in Cupertino, California.',
  'ceo': 'Mr. Timothy Cook',
  'sector': 'Technology',
  'country': 'US',
  'fullTimeEmployees': '154000',
  'phone': '14089961010',
  'address': '1 Apple Park Way',
  'city': 'Cupertino',
  'state': 'CALIFORNIA',
  'zip': '95014',
  'dcfDiff': 2.07176,
  'dcf': 155.112,
  'image': 'https://financialmodelingprep.com/image-stock/AAPL.png',
  'ipoDate': '1980-12-12',
  'defaultImage': False,
  'isEtf': False,
  'isActivelyTrading': True,
  'isAdr': False,
  'isFund': False}] 

I know it will be easier for me to finish my project if the json I receive is a dictionary instead of a list. How can I convert this list to a dictionary or just have a dictionary print every time I call the api?

Asked By: BannyM

||

Answers:

If it is always a one-item list then just call the first element in the list to get the dictionary.

data = requests.get(api_url).json()[0]

The robust way to handle this is to create a function that handles a single dictionary:

def response_handler(data: dict):
    # Do what you need to do

Then, when you get your list response, simply call the handler on each dictionary:

for data in response:
    response_handler(data)

That said, if you only ever need the first element, per the other responses, then you just need to requests.get(api_url).json()[0] to grab the first element. Note that you’re getting a list of dictionaries – and you’re only interested in a dictionary. So if the first element of that list is the dictionary you care about, you can just index into the list you have (the [0] subscript).

Answered By: Nathaniel Ford
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.