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

References <ad743135-a552-4cd6-ba1f-c492bf8bbb37@googlegroups.com>
Date 2014-01-16 20:48 +1100
Subject Re: Building and accessing an array of dictionaries
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5578.1389865714.18130.python-list@python.org> (permalink)

Show all headers | 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