python nested list to dictionary

python dictionary
python nested dictionary
convert list to dictionary python
pass list to dictionary python
python flatten dictionary of lists
how to combine two lists into a dictionary python
python get all values from nested dictionary
python convert list to dictionary with index

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', '$$']]

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'])

Nested list to dict, Nested list to dict · python list dictionary list-comprehension. I am trying to create dict by nested list : Here’s some sample data that includes nested lists, dictionaries, and values. When calling our flatten() function using the above data, here is the result using pprint() to cleanly format the

Here's a way to do it with a list comprehension, in vanilla python. Assuming the 2D list you gave is stored in my_list:

keys = ['name', 'stars', 'price']
my_dict = [dict(zip(keys, values)) for values in my_list]

The zip(k, v) takes two lists and maps them into a dictionary-like structure so that k is the keys, and each v is the corresponding values. You do need to cast the result to a dict, though.

Python, For this, simply declare a dictionary, and then run nested loop for both the lists and assign key and value pairs to from list values to dictionary. filter_none. edit What is Nested Dictionary in Python? In Python, a nested dictionary is a dictionary inside a dictionary. It's a collection of dictionaries into one single dictionary. nested_dict = { 'dictA': {'key_1': 'value_1'}, 'dictB': {'key_2': 'value_2'}} Here, the nested_dict is a nested dictionary with the dictionary dictA and dictB. They are two dictionary each having own key and value.

You can zip the keys of the desired sub-dicts with the corresponding values in a dict constructor (assuming your list is stored in variable l):

[dict(zip(('name', 'stars', 'price'), i)) for i in l]

This returns:

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

Python, Note that the restriction with keys in Python dictionary is only immutable data types can be Method #2: Adding nested list as value using append() method. Sometimes, while working with Python records, we can have problem in which we have data in form of Lists of list and we need to allocate certain elements as keys and certain as values to form a dictionary.

Using a simpler list comprehension, you can create the dict with:

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', '$$']]

my_dict = {'venues': [{'name': item[0], 'stars': item[1], 'price': item[2]} for item in list]}

my_dict_entries = my_dict['venues']

for i in range(len((my_dict_entries))):
    print(my_dict_entries[i]['name'])
    print(my_dict_entries[i]['stars'])
    print(my_dict_entries[i]['price'])

dict:

{"venues": [{"name": "CalaBar & Grill", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "Red Chili Cafe", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "Gus\u2019s World Famous Fried Chicken", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "South City Kitchen - Midtown", "rating": "4.5 star rating", "pricing": "$$"}, {"name": "Mary Mac\u2019s Tea Room", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "Busy Bee Cafe", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "Richards\u2019 Southern Fried", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "Greens & Gravy", "rating": "3.5 star rating", "pricing": "$$"}, {"name": "Colonnade Restaurant", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "South City Kitchen Buckhead", "rating": "4.5 star rating", "pricing": "$$"}, {"name": "Poor Calvin\u2019s", "rating": "4.5 star rating", "pricing": "$$"}, {"name": "Rock\u2019s Chicken & Fries", "rating": "4.0 star rating", "pricing": "$"}, {"name": "Copeland\u2019s", "rating": "3.5 star rating", "pricing": "$$"}]}

dict_entries:

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

Output [truncated]:

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
$$
...

This will give you a more robust venues dict structure that allows you to address your list well. For example, my_dict_entries gives you the list your looking for in your question.

How To Flatten a Dictionary With Nested Lists and Dictionaries in , How To Flatten a Dictionary With Nested Lists and Dictionaries in Python. Learn to flatten a dictionary with a custom separator, accommodating  One way to add a dictionary in the Nested dictionary is to add values one be one, Nested_dict[dict][key] = 'value'. Another way is to add the whole dictionary in one go, Nested_dict[dict] = { 'key': 'value'} .

Python: Convert a list into a nested dictionary of keys, Python Exercises, Practice and Solution: Write a Python program to convert a list into a nested dictionary of keys. Python Nested Dictionary A dictionary can contain another dictionary, which in turn can contain dictionaries themselves, and so on to arbitrary depth. This is known as nested dictionary. Nested dictionaries are one of many ways to represent structured information (similar to ‘records’ or ‘structs’ in other languages).

5. Data Structures, List comprehensions can contain complex expressions and nested functions: It is best to think of a dictionary as a set of key: value pairs, with the requirement  Converting a list to dictionary with list elements as keys in dictionary. using dict.fromkeys () '''. dictOfWords = dict.fromkeys(listOfStr , 1) dict.fromKeys () accepts a list and default value. It returns a dictionary with items in list as keys. All dictionary items will have same value, that was passed in fromkeys ().

Python : Convert list of lists or nested list to flat list – thispointer.com, Python : How to convert a list to dictionary ? Python: numpy.ravel() function Tutorial with examples · Pandas : Convert a DataFrame into a list of  Python Exercises, Practice and Solution: Write a Python program to sort a list of nested dictionaries.

Comments
  • It would help to know what you've tried so far and how it fell short of your expectations
  • a dictionary of dictionaries whose top-level keys are integers is effectively the same as a list of dictionaries. my recommendation to the OP is the leave it as you've done it (a list of dicts)
  • (technically that's not a generator)
  • @PaulH it is technically a comprehension rather than a generator, but the distinction isn't really important in this case
  • @PaulH Indeed. Was actually using a generator expression in a different solution while writing this answer but switched my approach. Corrected then.
  • @GreenCloakGuy agreed entirely, hence the italics and ()