adi99
adi99

Reputation: 73

Nesting lists in dictionaries

I'd like to know the limitations regarding nesting in Dictionaries and Lists. The following code works fine.

dict = {
        'apples': {'typ1':'2.99','typ2':'2.49'},
        'oranges': '1.99',
        'berries':['blue', 'green', 'red']
       }

But if I try to create a dictionary inside the list under the key 'berries' like this:

dict={
        'apples': {'typ1':'2.99','typ2':'2.49'}, 
        'oranges':'1.99', 
        'berries': [ blue = {'typ1':'3.99','typ2'='3.49'}, 'green', 'red']
     }

It doesn't work. Can someone please explain?

Upvotes: 0

Views: 62

Answers (3)

VickNguyen
VickNguyen

Reputation: 11

First of all, I see you create wrong format. You must follow the json format.
Wrong format
'berries': [blue = {'typ1': '3.99', 'typ2' = '3.49'}, 'green', 'red']

so the above element will be modified as follows:
'berries': [{'blue': {'typ1': '3.99', 'typ2': '3.49'}}, 'green', 'red']}

Also the type of the berries element is list so you must use the "append" function to add a new element. So the correct way is:
dict['berries'].append({'blue': {'typ1': '3.99', 'typ2': '3.49'}})

Summary code:

dict = {'apples': {'typ1': '2.99', 'typ2': '2.49'}, 'oranges': '1.99', 'berries': ['green', 'red']}
dict['berries'].append({'blue': {'typ1': '3.99', 'typ2': '3.49'}})
print(dict)

The expected result is: {'apples': {'typ1': '2.99', 'typ2': '2.49'}, 'oranges': '1.99', 'berries': ['green', 'red', {'blue': {'typ1': '3.99', 'typ2': '3.49'}}]}

Upvotes: 0

pho
pho

Reputation: 25490

First, don't call your dictionaries dict. This obscures the actual class dict in Python and makes it so that you can no longer refer to that class.

Now, 'berries': [ blue = {'typ1':'3.99','typ2'='3.49'}, 'green', 'red'] is the part that messes everything up because the syntax is invalid.

Dictionaries, defined using {...} have keys and values.

Lists, defined using [...] cannot have keys. Lists only have elements.

So you could define a list like ['blue', 'green', 'red'] and assign that list to the key 'berries' like so:

mydict = { ...
         , 'berries': ['blue', 'green', 'red']
         }

You could even mix up the types of elements in the list, so this is also valid: [{'color': 'blue', 'typ1': 3.99, 'typ2': 3.49}, 'green', 'red']

Or you could define a dictionary like {'blue': {...}, 'green': {...}, 'red': {...}} and assign that dict to the key 'berries' like so:

mydict = { ...
         , 'berries': {'blue': {...}, 'green': {...}, 'red': {...}}
         }

But you cannot assign a key-value pair in a list like you tried to do originally. Moreover, the = symbol is the wrong thing to use for a key-value pair anyway.

Upvotes: 3

mzndr
mzndr

Reputation: 91

[blue={'typ1':'3.99','typ2'='3.49'},'green', ...] this makes no sense. You're trying to assign something inside the array with the = operator. Its not a limitation, just wrong syntax. Also, changing the = to a : won't work aswell, because an array doesn't hold key-value pairs. So it seems like you're mixing up dictionaries and arrays.

Upvotes: 3

Related Questions