Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64068

Re: Building and accessing an array of dictionaries

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
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; '16,': 0.03; 'syntax': 0.04; 'lst': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; 'dict': 0.16; 'dictionary.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'helps!': 0.16; 'instead:': 0.16; 'subject:accessing': 0.16; 'subject:array': 0.16; 'subject:dictionaries': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'example': 0.22; 'cc:addr:python.org': 0.22; 'skip:{ 20': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'array': 0.29; 'message- id:@mail.gmail.com': 0.30; 'received:google.com': 0.35; 'list': 0.37; 'pm,': 0.38; 'most': 0.60; 'hope': 0.61; 'sam': 0.68; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=tqLLBs4zJtLO7xEbQqH/tBCiMU8mof/jqDSdVo/F/+o=; b=Y3NS4aOBicHuG5Kwcawp1SBcAqD/vtSSYOBh+OGWxppU3qpMO5RKQfMg6sS0ZO946k aDlpyHwExQEOVCPP1j0NAmGwaJ35uzgdwtCOUXL3RzTLYgUOCP1PlifmWNyeRmAy+bJy fJJhV5a8T2MRxqL8yQyl9BPG76FSjwb1GfTvinzo22NFnGGZ/uKbSNNn3yT4omWGdnPm 7G00HsZYUfjvjUQXvNGQWP+9owmf1i8k/Sml44PrRF0JYnGeFn7/HJinDTyz38h0XIet qifba5TH89mThDCgGWkVSAVXbOKaSTWzM2J2jxx5swy8/tECac8FxrdVI7wNO29/DMjG OQlQ==
MIME-Version 1.0
X-Received by 10.66.102.39 with SMTP id fl7mr8697503pab.43.1389865711550; Thu, 16 Jan 2014 01:48:31 -0800 (PST)
In-Reply-To <ad743135-a552-4cd6-ba1f-c492bf8bbb37@googlegroups.com>
References <ad743135-a552-4cd6-ba1f-c492bf8bbb37@googlegroups.com>
Date Thu, 16 Jan 2014 20:48:31 +1100
Subject Re: Building and accessing an array of dictionaries
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5578.1389865714.18130.python-list@python.org> (permalink)
Lines 25
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389865714 news.xs4all.nl 2907 [2001:888:2000:d::a6]:47518
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64068

Show key headers only | View raw


On Thu, Jan 16, 2014 at 8:41 PM, Sam <lightaiyee@gmail.com> wrote:
> I would like to build an array of dictionaries. Most of the dictionary example on the net are for single dictionary.
>
> dict = {'a':'a','b':'b','c':'c'}
> dict2 = {'a':'a','b':'b','c':'c'}
> dict3 = {'a':'a','b':'b','c':'c'}
>
> arr = (dict,dict2,dict3)
>
> What is the syntax to access the value of dict3->'a'?

Technically, that's a tuple of dictionaries, and you may want to use a
list instead:

lst = [dict, dict2, dict3]

Like any other list or tuple, you can reference them by their indices:

lst[2] is dict3

lst[2]['a'] is dict3['a']

Hope that helps!

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Building and accessing an array of dictionaries Sam <lightaiyee@gmail.com> - 2014-01-16 01:41 -0800
  Re: Building and accessing an array of dictionaries Chris Angelico <rosuav@gmail.com> - 2014-01-16 20:48 +1100
  Re: Building and accessing an array of dictionaries Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-01-16 11:52 +0200
  Re: Building and accessing an array of dictionaries Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-16 17:01 +0000
    Re: Building and accessing an array of dictionaries Grant Edwards <invalid@invalid.invalid> - 2014-01-17 20:53 +0000

csiph-web