Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; 'puts': 0.07; 'collections': 0.09; 'dict': 0.09; 'subject:Why': 0.09; 'tuple.': 0.09; 'python': 0.11; 'defaultdict': 0.16; 'dictionaries': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'people:': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:dictionaries': 0.16; 'task.': 0.16; 'wrote:': 0.16; 'creates': 0.18; '(not': 0.20; 'trying': 0.22; 'see:': 0.22; 'references': 0.23; "i've": 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'skip:# 10': 0.27; "doesn't": 0.28; 'subject:list': 0.28; "i'm": 0.29; 'work.': 0.30; 'help!': 0.31; 'initially': 0.31; 'print': 0.31; 'code': 0.31; 'skip:[ 10': 0.32; 'received:84': 0.32; 'structure': 0.32; 'subject:?': 0.34; 'to:addr:python-list': 0.35; 'list:': 0.35; 'list': 0.35; 'but': 0.36; 'list,': 0.36; 'thanks': 0.36; 'skip:{ 10': 0.36; 'subject:work': 0.36; 'hi,': 0.37; 'subject:: ': 0.37; 'thought': 0.37; 'skip:p 20': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'data': 0.40; 'some': 0.40; "you'll": 0.61; 'default': 0.61; 'dict.': 0.84; 'subject:this': 0.85 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=OoyysHLt c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=QrohdLjRRo4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=JtgHjuMwhUZWTexguzsA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Thu, 18 Jun 2015 19:37:40 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Why this list of dictionaries doesn't work? References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1434652666 news.xs4all.nl 2900 [2001:888:2000:d::a6]:39676 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92842 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(, {'name': 'kacey', 'age': 18, 'nome': {}}), defaultdict(, {'name': 'kacey', 'age': 18, 'nome': {}})]