Why this list of dictionaries doesn't work?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Why this list of dictionaries doesn't work?

Gilcan Machado
Hi,

I'm trying to write a list of dictionaries like:

people = (
         {'name':'john', 'age':12} ,
         {'name':'kacey', 'age':18}
    )


I've thought the code below would do the task.

But it doesn't work.

And if I "print(people)" what I get is not the organize data structure like
above.

Thanks of any help!
[]s
Gilcan

#!/usr/bin/env python
from collections import defaultdict

person = defaultdict(dict)
people = list()

person['name'] = 'jose'
person['age'] = 12

people.append(person)

person['name'] = 'kacey'
person['age'] = 18

people.append(person)

for person in people:
    print( person['nome'] )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150618/8a148e82/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Why this list of dictionaries doesn't work?

Gary Herron-2
On 06/18/2015 10:57 AM, Gilcan Machado wrote:

> Hi,
>
> I'm trying to write a list of dictionaries like:
>
> people = (
>          {'name':'john', 'age':12} ,
>          {'name':'kacey', 'age':18}
>     )
>
>
> I've thought the code below would do the task.
>
> But it doesn't work.

Never say "it doesn't work" on this list.  Tell us what it did, and what
you expected.  Provide a copy of the full error message. That's much
more helpful than making us guess.

>
> And if I "print(people)" what I get is not the organize data structure
> like above.
>
> Thanks of any help!
> []s
> Gilcan
>
> #!/usr/bin/env python
> from collections import defaultdict
>
> person = defaultdict(dict)

If you want *two* different dictionaries, you'll have to create *two* of
them.  You code creates only this one.

> people = list()
>
> person['name'] = 'jose'
> person['age'] = 12
>
> people.append(person)

Here's where you need to create the second one.

>
> person['name'] = 'kacey'
> person['age'] = 18
>
> people.append(person)
>
> for person in people:
>     print( person['nome'] )

Typo here:  'name', not 'nome'.

>
>
>


--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150618/8b88d538/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Why this list of dictionaries doesn't work?

MRAB-2
In reply to this post by Gilcan Machado
On 2015-06-18 18:57, Gilcan Machado wrote:
> Hi,
>
> I'm trying to write a list of dictionaries like:
>
> people = (
>           {'name':'john', 'age':12} ,
>           {'name':'kacey', 'age':18}
>      )
>
That's not a list; it's a tuple. If you want a list, use '[' and ']'.

>
> I've thought the code below would do the task.
>
> But it doesn't work.
>
> And if I "print(people)" what I get is not the organize data structure
> like above.
>
> Thanks of any help!
> []s
> Gilcan
>
> #!/usr/bin/env python
> from collections import defaultdict
>
You don't need a defaultdict, just a normal dict.

This creates an empty defaultdict whose default value is a dict:

> person = defaultdict(dict)
> people = list()
>
This puts some items into the dict:

> person['name'] = 'jose'
> person['age'] = 12
>
This puts the dict into the list:

> people.append(person)
>
This _reuses_ the dict and overwrites the items:

> person['name'] = 'kacey'
> person['age'] = 18
>
This puts the dict into the list again:

> people.append(person)
>
The list 'people' now contains 2 references to the _same_ dict.

> for person in people:
>      print( person['nome'] )

Initially there's no such key as 'nome' (not the spelling), so it
creates one with the default value, a dict.

If you print out the people list, you'll see:

[defaultdict(<class 'dict'>, {'name': 'kacey', 'age': 18, 'nome': {}}),
defaultdict(<class 'dict'>, {'name': 'kacey', 'age': 18, 'nome': {}})]