Re: Building and accessing an array of dictionaries

2014-01-17 Thread Grant Edwards
On 2014-01-16, Mark Lawrence  wrote:
> On 16/01/2014 09:48, Chris Angelico wrote:
>> On Thu, Jan 16, 2014 at 8:41 PM, Sam  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
>
> For the benefit of lurkers, newbies or whatever it's the commas that 
> make the tuple, not the brackets.  

In _that_ example, yes.  There are other cases where it's the
brackets (sort of):

foo('a','b','c')# three separate string objects are passed

foo(('a','b','c'))  # a single tuple object is passed

-- 
Grant Edwards   grant.b.edwardsYow! Being a BALD HERO
  at   is almost as FESTIVE as a
  gmail.comTATTOOED KNOCKWURST.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Building and accessing an array of dictionaries

2014-01-16 Thread Mark Lawrence

On 16/01/2014 09:48, Chris Angelico wrote:

On Thu, Jan 16, 2014 at 8:41 PM, Sam  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


For the benefit of lurkers, newbies or whatever it's the commas that 
make the tuple, not the brackets.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

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


Re: Building and accessing an array of dictionaries

2014-01-16 Thread Jean-Michel Pichavant
- 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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Building and accessing an array of dictionaries

2014-01-16 Thread Jussi Piitulainen
Sam writes:

> 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'?

This isn't a special case.

arr[2] to get the dictionary
arr[2]['a'] to get the value in the dictionary

'a' in arr[2] to find if there is such a key

arr[2].get('a') to get the value or None if the key isn't there
arr[2].get('a', 'd') to get a value even if the key isn't there

help(dict.get)

for key in arr[2]:
   # to iterate over the keys

The exact same mechanisms are used no matter where you get the
dictionary from.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Building and accessing an array of dictionaries

2014-01-16 Thread Chris Angelico
On Thu, Jan 16, 2014 at 8:41 PM, Sam  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
-- 
https://mail.python.org/mailman/listinfo/python-list


Building and accessing an array of dictionaries

2014-01-16 Thread Sam
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