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


Groups > comp.lang.python > #64070 > unrolled thread

Re: Building and accessing an array of dictionaries

Started byJean-Michel Pichavant <jeanmichel@sequans.com>
First post2014-01-16 10:57 +0100
Last post2014-01-16 10:57 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Re: Building and accessing an array of dictionaries Jean-Michel Pichavant <jeanmichel@sequans.com> - 2014-01-16 10:57 +0100

#64070 — Re: Building and accessing an array of dictionaries

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2014-01-16 10:57 +0100
SubjectRe: Building and accessing an array of dictionaries
Message-ID<mailman.5579.1389866361.18130.python-list@python.org>
----- Original Message -----
> 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'?
> 
> Thank you.
> 
> --
> https://mail.python.org/mailman/listinfo/python-list
> 

Hi,

arr = (dict,dict2,dict3) 
builds a tuple.

If you want to build a (ordered) List, which is the closest type to array (arrays don't exists in python), you may write

myList = [dict, dict2, dict3]

you can access 'a' by writing 

myList[2]['a']
Additionally:
myList[0] -> 1st element
myList[-1] -> last element
myList[3:] -> list of elements of myList from the the 4th element to the last


Accessing a list element or a dictionary value is done through the same operator []. That can be confusing at the very beginning, you'll get used to it eventually.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web