-1

I have a list of lists that I would like to convert into:

dict[i]['name']
dict[i]['stars']
dict[i]['numrevs']
dict[i]['price']

dict[0] tells me about the first column of data (Gus’s World Famous Fried Chicken)

the inner lists are as follows:

  1. restaurant name (name)
  2. star rating (stars)
  3. number of reviews (numrevs)
  4. price (price)
bigList = [
    ['Gus’s World Famous Fried Chicken', 'South City Kitchen - Midtown', 'Mary Mac’s Tea Room', 'Busy Bee Cafe', 'Richards’ Southern Fried', 'Greens & Gravy', 'Colonnade Restaurant', 'South City Kitchen Buckhead', 'Poor Calvin’s', 'Rock’s Chicken & Fries', 'Copeland’s'],
    ['4.0', '4.5', '4.0', '4.0', '4.0', '3.5', '4.0', '4.5', '4.5', '4.0', '3.5'],
    [549, 1777, 2241, 481, 108, 93, 350, 248, 1558, 67, 288],
    ['$$', '$$', '$$', '$$', '$$', '$$', '$$', '$$', '$$', '$', '$$']]

Any help would really be appreciated.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
PDubs
  • 7
  • 3
  • 2
    This is not hard. `zip(*bigList)` will help. What hat you tried? This isn't a code-writing service. – Tim Roberts Sep 24 '21 at 04:10
  • Similar questions: [Convert list of lists to list of dictionaries](/q/35763593/4518341), [Convert list of lists to list of dictionaries based on order of items in sublist](/q/52273841/4518341) – wjandrea Sep 24 '21 at 04:25

1 Answers1

1
  1. use zip to transpose your lists
  2. call dict with the keys and the transposed list zipped together

then that becomes

keys = ["name","stars","numReviews","price"]
myDicts = [dict(zip(keys, row)) for row in zip(*bigList)]

this basically is just a list comprehension that does the following

dict([('name',"Some Name"),("stars", 5),("numReviews",50),("price","expensive")])

and will result in a list of the above dicts

[{"name":"Some Name",...},{"name":"Other name",...}, ...]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179