Search code examples
pythondictionaryconcatenation

python how to concatenate two dictionaries


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)

Solution

  • You can create a dictionary with your new key, "companyProfiles", mapped to an empty list. Then, on each iteration (for each symbol), you can append your JSON response dictionary to this list, i.e.:

    import json
    import requests
    tempdict = {"companyProfiles": []}
    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["companyProfiles"].append(data)
    print(tempdict)
    

    Outputs:

    {'companyProfiles': [{'profile': {'beta': '1.228499',
                                      'ceo': 'Timothy D. Cook',
                                      'changes': -0.52,
                                      'changesPercentage': '(-0.20%)',
                                      'companyName': 'Apple Inc.',
                                      '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.',
                                      'exchange': 'Nasdaq Global Select',
                                      'image': 'https://financialmodelingprep.com/images-New-jpg/AAPL.jpg',
                                      'industry': 'Computer Hardware',
                                      'lastDiv': '2.92',
                                      'mktCap': '1.11264072E12',
                                      'price': 254.29,
                                      'range': '170.27-327.85',
                                      'sector': 'Technology',
                                      'volAvg': '49330169',
                                      'website': 'http://www.apple.com'},
                          'symbol': 'AAPL'},
                         {'profile': {'beta': '1.062394',
                                      'ceo': 'Mark Zuckerberg',
                                      'changes': 0.85,
                                      'changesPercentage': '(+0.51%)',
                                      'companyName': 'Facebook Inc.',
                                      '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.',
                                      'exchange': 'Nasdaq Global Select',
                                      'image': 'https://financialmodelingprep.com/images-New-jpg/FB.jpg',
                                      'industry': 'Online Media',
                                      'lastDiv': '0',
                                      'mktCap': '4.75455062E11',
                                      'price': 166.8,
                                      'range': '137.1-224.2',
                                      'sector': 'Technology',
                                      'volAvg': '21198830',
                                      'website': 'http://www.facebook.com'},
                          'symbol': 'FB'}]}