After looking for a long time, everyone was directing the answer to this post: [How do I merge two dictionaries in a single expression?
But I am looking to concatenate the two dictionaries and not merge.
Both of my dictionaries have the same key so when I use tempdict.update(data)
, it replaces my data instead of adding it to it.
The dictionaries looks like this:
{
"symbol" : "AAPL",
"profile" : {
"price" : 254.29,
"beta" : "1.228499",
"volAvg" : "49330169",
"mktCap" : "1.11264072E12",
"lastDiv" : "2.92",
"range" : "170.27-327.85",
"changes" : -0.52,
"changesPercentage" : "(-0.20%)",
"companyName" : "Apple Inc.",
"exchange" : "Nasdaq Global Select",
"industry" : "Computer Hardware",
"website" : "http://www.apple.com",
"description" : "Apple Inc is designs, manufactures and markets mobile communication and media devices and personal computers, and sells a variety of related software, services, accessories, networking solutions and third-party digital content and applications.",
"ceo" : "Timothy D. Cook",
"sector" : "Technology",
"image" : "https://financialmodelingprep.com/images-New-jpg/AAPL.jpg"
}
}
{
"symbol" : "FB",
"profile" : {
"price" : 166.8,
"beta" : "1.062394",
"volAvg" : "21198830",
"mktCap" : "4.75455062E11",
"lastDiv" : "0",
"range" : "137.1-224.2",
"changes" : 0.85,
"changesPercentage" : "(+0.51%)",
"companyName" : "Facebook Inc.",
"exchange" : "Nasdaq Global Select",
"industry" : "Online Media",
"website" : "http://www.facebook.com",
"description" : "Facebook Inc is the world's largest online social network. Its products are Facebook, Instagram, Messenger, WhatsApp, and Oculus. Its products enable people to connect and share through mobile devices and personal computers.",
"ceo" : "Mark Zuckerberg",
"sector" : "Technology",
"image" : "https://financialmodelingprep.com/images-New-jpg/FB.jpg"
}
}
I want it to look like this:
{
"companyProfiles" : [ {
"symbol" : "AAPL",
"profile" : {
"price" : 254.29,
"beta" : "1.228499",
"volAvg" : "49330169",
"mktCap" : "1.11264072E12",
"lastDiv" : "2.92",
"range" : "170.27-327.85",
"changes" : -0.52,
"changesPercentage" : "(-0.20%)",
"companyName" : "Apple Inc.",
"exchange" : "Nasdaq Global Select",
"industry" : "Computer Hardware",
"website" : "http://www.apple.com",
"description" : "Apple Inc is designs, manufactures and markets mobile communication and media devices and personal computers, and sells a variety of related software, services, accessories, networking solutions and third-party digital content and applications.",
"ceo" : "Timothy D. Cook",
"sector" : "Technology",
"image" : "https://financialmodelingprep.com/images-New-jpg/AAPL.jpg"
}
}, {
"symbol" : "FB",
"profile" : {
"price" : 166.8,
"beta" : "1.062394",
"volAvg" : "21198830",
"mktCap" : "4.75455062E11",
"lastDiv" : "0",
"range" : "137.1-224.2",
"changes" : 0.85,
"changesPercentage" : "(+0.51%)",
"companyName" : "Facebook Inc.",
"exchange" : "Nasdaq Global Select",
"industry" : "Online Media",
"website" : "http://www.facebook.com",
"description" : "Facebook Inc is the world's largest online social network. Its products are Facebook, Instagram, Messenger, WhatsApp, and Oculus. Its products enable people to connect and share through mobile devices and personal computers.",
"ceo" : "Mark Zuckerberg",
"sector" : "Technology",
"image" : "https://financialmodelingprep.com/images-New-jpg/FB.jpg"
}
} ]
}
This is what I have got so far, but I know the problem is with the .update
:
tempdict = dict()
for symbol in ["AAPL","FB"]:
with requests.get("https://financialmodelingprep.com/api/v3/company/profile/"+ symbol) as url:
data_string = url.content.decode()
data =json.loads(data_string)
tempdict.update(data)
print(tempdict)