Re: Why this list of dictionaries doesn't work?

2015-06-18 Thread Steven D'Aprano
On Fri, 19 Jun 2015 03: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.

Why don't you just use exactly what you have above? Just change the round
brackets (parentheses) to square brackets to change people from a tuple to
a list:

people = [{'name': 'john', 'age': 12}, {'name': 'kacey', 'age': 18}]

and you now have a list of two dicts.



 But it doesn't work.

What does it do? Does the computer catch fire? Blue Screen Of Death? Does
Python crash, or start printing All work and no play makes Jack a dull
boy? over and over again?

*wink*

You need to explain what you expected to happen and what actually happened,
not just it doesn't work.


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

Remember that Python doesn't *pretty-print* lists or dicts by default. If
you print people, you'll get all the information, but it may not be
displayed in what you consider a particularly pleasing or organized manner:

py people = [{'name': 'john', 'age': 12}, {'name': 'kacey', 'age': 18}]
py print(people)
[{'age': 12, 'name': 'john'}, {'age': 18, 'name': 'kacey'}]


If you want it displayed as in your original sample, you will need to write
your own print function.


 #!/usr/bin/env python
 from collections import defaultdict
 
 person = defaultdict(dict)
 people = list()
 
 person['name'] = 'jose'
 person['age'] = 12
 
 people.append(person)

This is a little different from what you have above. It uses a defaultdict
instead of a regular dict, I presume you have some good reason for that.


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

This, however, if where you go wrong. You're not creating a second dict, you
are modifying the existing one. When you modify a dict, you modify it
everywhere it appears. So it doesn't matter whether you look at the
variable person, or if you look at the list people containing that dict,
you see the same value:

print(person)
print(people[0])

Both print the same thing, because they are the same dict (not mere copies).

Another way to put it, Python does *not* copy the dict when you insert it
into a list. So whether you look at people or person[0], you are looking at
the same object.

 people.append(person)

And now you append the same dict to the list, so it is in the list twice.
Now you have:

person
people[0]
people[1]


being three different ways to refer to the same dict, not three different
dicts.



-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why this list of dictionaries doesn't work?

2015-06-18 Thread MRAB

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': {}})]


--
https://mail.python.org/mailman/listinfo/python-list


Why this list of dictionaries doesn't work?

2015-06-18 Thread 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'] )
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why this list of dictionaries doesn't work?

2015-06-18 Thread Gary Herron

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

-- 
https://mail.python.org/mailman/listinfo/python-list