python nested list to dictionary

zelda1234

what is the best way to convert this list into a dictionary (call it my_dict) so that it can be indexed this way?

my_dict[i]['name']
my_dict[i]['stars']
my_dict[i]['price']

Basically my_dict[0] would give me everything about 'CalaBar & Grill'.

Here's the list:

[['CalaBar & Grill', '4.0 star rating', '$$'],
 ['Red Chili Cafe', '4.0 star rating', '$$'],
 ['Gus’s World Famous Fried Chicken', '4.0 star rating', '$$'],
 ['South City Kitchen - Midtown', '4.5 star rating', '$$'],
 ['Mary Mac’s Tea Room', '4.0 star rating', '$$'],
 ['Busy Bee Cafe', '4.0 star rating', '$$'],
 ['Richards’ Southern Fried', '4.0 star rating', '$$'],
 ['Greens & Gravy', '3.5 star rating', '$$'],
 ['Colonnade Restaurant', '4.0 star rating', '$$'],
 ['South City Kitchen Buckhead', '4.5 star rating', '$$'],
 ['Poor Calvin’s', '4.5 star rating', '$$'],
 ['Rock’s Chicken & Fries', '4.0 star rating', '$'],
 ['Copeland’s', '3.5 star rating', '$$']]
shariful

This should work:

# The list
my_list = [['CalaBar & Grill', '4.0 star rating', '$$'], \
 ['Red Chili Cafe', '4.0 star rating', '$$'],\
 ['Gus’s World Famous Fried Chicken', '4.0 star rating', '$$'],\
 ['South City Kitchen - Midtown', '4.5 star rating', '$$'],\
 ['Mary Mac’s Tea Room', '4.0 star rating', '$$'],\
 ['Busy Bee Cafe', '4.0 star rating', '$$'],\
 ['Richards’ Southern Fried', '4.0 star rating', '$$'],\
 ['Greens & Gravy', '3.5 star rating', '$$'],\
 ['Colonnade Restaurant', '4.0 star rating', '$$'],\
 ['South City Kitchen Buckhead', '4.5 star rating', '$$'],\
 ['Poor Calvin’s', '4.5 star rating', '$$'],\
 ['Rock’s Chicken & Fries', '4.0 star rating', '$'],\
 ['Copeland’s', '3.5 star rating', '$$']]

# initialize an empty list
my_dict = []

# create list of dictionary
for elem in my_list:
    temp_dict = {}
    temp_dict['name'] = elem[0]
    temp_dict['stars'] = elem[1]
    temp_dict['price'] = elem[2]
    my_dict.append(temp_dict)


# testing
print(my_dict[1]['stars'])
print(my_dict[5]['price'])
print(my_dict[0]['name'])
print(my_dict[7]['stars'])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting a nested list of dictionary in Python

How to create a nested dictionary from a list in Python?

List to nested dictionary in python

Building a nested python Dictionary from list

Python: List Nested Dictionary to pandas DataFrame Issue

Python. List to nested dictionary: strange behaviour

Python - Converting nested list into dictionary

Python merge nested list of dictionary to single list of dictionary

Python loop through nested dictionary or dictionaries in a list

Flattening nested dictionary with possible list of dictionaries in python

Generate Dictionary from nested List, Python 3.6

Nested Dictionary list of Dictonaries retrieving value in Python

Nested Dictionary Comprehension in Python with List of Values

Nested list to nested dictionary in python

Python: nested list and dictionary comparison

List Comprehension for Ordered Nested Dictionary to Python List

Manipulate a Nested Dictionary/List Tree in Python 3

Python add to list in nested dictionary

Python get nested dictionary value inside of list

Python pandas: Nested List of Dictionary into Dataframe

Python: Nested List Inside Dictionary?

Python modify nested dictionary value at list of keys

Converting nested list and a list of tuples into a dictionary python

List in nested dictionary in Python

nested dictionary in list to dataframe python

How to map a nested list to a iterable dictionary in Python

Python create dictionary from nested class in list

Convert a nested Dictionary to a nested List with List Comprehension method in Python

Create automatically list in a nested dictionary python