-2

How do I add data to the "second depth" of a dictionary. I am pulling data from the Alphavantage API and the data comes out in a dictionary format like this:

{'2021-01-28 20:00:00': {'1. open': '68.6200', '2. high': '68.8000', '3. low': '68.5500', '4. close': '68.7000', '5. volume': '5032'}, '2021-01-28 19:00:00': {'1. open': '68.7200', '2. high': '68.7200', '3. low': '68.5500', '4. close': '68.5700', '5. volume': '4602'}

I would like to add '6. ticker' and my ticker variable to each 2nd level dictionary.

Here is my code, it requires an API key to use, but the data that comes out is pretty simple, I'm just lost when it comes to how to use dictionaries, I learned most coding using relational DBs and dictionaries just confuse and frighten me.

    from alpha_vantage.timeseries import TimeSeries

import json

mykey = 'abc123'
ticker = 'GOOG'


app = TimeSeries(mykey)

alpha_vantage_api_key = mykey

data, metadata = app.get_intraday(symbol=ticker, interval= '60min', outputsize='json')

# data['6. Ticker'] = "PINS"

print(data)

1 Answers1

2
for key in data.keys():
    data[key]['6. Ticker'] = WHATEVER_VALUE_YOU_WANT

This will loop through all the keys (dates in this case) in the first layer dictionary and add/set the new 6th key to the value you assign.

Does this make sense?