Home > Net >  How to merge lists of different sizes into a dictionary from API?
How to merge lists of different sizes into a dictionary from API?

Time:09-22

I have 2 lists from an API, and I want to have one be the keys and the other be the values for a dictionary. The Keys and Values are not even, most keys have multiple values assigned to them; but every time I try to assign the values to the specific keys that the API gave me, I cannot figure out how to align the keys and values together and add it to the dictionary.

x = response_dict['businesses']

nameL = []
c2L = []
#a2 = collections.defaultdict(list)
a = {}
a2 = {}

for biz in response_dict['businesses']:
  print(biz['name'])
  nameL.append(biz['name'])
  
  for naming in biz['categories']:
    print(naming['title'])
    c2L.append(naming['title'])

Output for Printing print(biz['name']) and print(naming['title']) in for loop:

Avalon Grille
American (New)
-----
Maggie's Blue Rose
Mexican
-----
Eatalian 
Italian
Pizza
-----
The Kettle
Diners
American (Traditional)
-----
Raffaello Ristorante
Italian
-----
Steve's Steakhouse
Steakhouses
Seafood
Bars
-----
Mi Casita
Mexican
-----
North End Caffe
American (New)
Cafes
-----
Bowl Thai
Thai
Salad
Coffee & Tea
-----
The Garden Thai Restaurant
Thai
-----
Zacatecas Restaurant
Mexican
Seafood
Breakfast & Brunch
-----
Bubba Gump Shrimp
Seafood
Southern
-----
La Pasta
Italian
Pizza
Wine Bars
-----
La Bella Napoli
Pizza
Italian
Desserts
-----
The Chowder Barge
Seafood
Bars
American (Traditional)
-----
Oh My Burger
Burgers
Salad
Tacos
-----
Gatten Sushi
Japanese
Sushi Bars
-----
The Arthur J
Steakhouses
Cocktail Bars
-----
McDonald's
Fast Food
Burgers
Coffee & Tea
-----
Chubby Rice
Chinese
Asian Fusion
-----

I want the results to be in a dictionary form of the above:

{'Avalon Grille':['American (New)'], 'Maggie's Blue Rose':['Mexican'], 'Eatalian': ['Italian', 'Pizza'], ... 'McDonald's':['Fast Food', 'Burgers', 'Coffee & Tea']}

CodePudding user response:

Assuming that the input is the same as is being used in the code example at the top of your question, it seems that you are looking for something like this:

output = {}
for biz in response_dict["businesses"]:
    titles = []
    for category in biz["categories"]:
        titles.append(category["title"])
    output[biz["name"]] = titles

I would be tempted to condense it using comprehensions as such:

output = {biz["name"] : [category["title"] for category in biz["categories"]] for biz in response_dict["businesses"]}

CodePudding user response:

You can use defaultdict:

from collections import defaultdict
name_titles = defaultdict(list)
for biz in response_dict['businesses']:
    for naming in biz['categories']:
        name_titles[biz['name']].append(naming['title'])

Or list comprehension:

name_titles = {biz['name']: [naming['title'] for naming in biz['categories']] for biz in response_dict['businesses']}

CodePudding user response:

You can do it with a dictionary comprehension and a nested list comprehension.

result = {biz['name']: [naming['title'] for naming in biz['categories']] 
          for biz in response_dict['businesses']}
  • Related