Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > dictionaries with nested lists

Reply
Thread Tools

dictionaries with nested lists

 
 
Alex Martelli
Guest
Posts: n/a
 
      11-01-2003
ruari mactaggart wrote:

> can i write
>>>>dictionary[key][list][3]

>
> to mean the 3rd item in the list that is the corresponding value for 'key'
> ?


No, but you can write dictionary[key][2] for that purpose:

-- that[list] notation is unneeded and unsupported,
-- indices start from 0 so the 3rd one is indexed as [2]


Alex

 
Reply With Quote
 
 
 
 
Joe Francia
Guest
Posts: n/a
 
      11-01-2003
ruari mactaggart wrote:
> can i write
>
>>>>dictionary[key][list][3]

>
> to mean the 3rd item in the list that is the corresponding value for 'key' ?


You don't need the[list] part. Assuming you mean something like:

bands = {'beatles': ['john','paul','george','ringo']}

....then bands['beatles'][3] == 'ringo'

Peace,
Joe

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      11-01-2003
ruari mactaggart wrote:

> what if i mean
>

bands={'beatles':['john','paul','george','ringo'],['guitar','bass','guitar',
> 'drums']}


Invalid syntax.

> then i need to put bands['beatles'][1][3]='percussion'


If you mean that bands is a dictionaries of tuples of lists, yes, but you
need parentheses around the tuples (and I would suggest some formatting
for greater readability), e.g.:

bands = {'beatles': ( ['john', 'paul', 'george', 'ringo'],
['guitar', 'bass', 'guitar', 'drums'] )
}


Alex

 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      11-01-2003
[ruari mactaggart]
> what if i mean
> bands={'beatles':['john','paul','george','ringo'],['guitar','bass','guitar',
> 'drums']}
>
> then i need to put bands['beatles'][1][3]='percussion'
>
> i presume... ?


No need to presume.
Just fire up the interpreter and see.

Compared to comp.lang.python responders,
the results are instant, always accurate, and
immediately available for exploring alternatives.


Raymond Hettinger


 
Reply With Quote
 
ruari mactaggart
Guest
Posts: n/a
 
      11-02-2003
can i write
>>>dictionary[key][list][3]


to mean the 3rd item in the list that is the corresponding value for 'key' ?


 
Reply With Quote
 
ruari mactaggart
Guest
Posts: n/a
 
      11-02-2003
what if i mean
bands={'beatles':['john','paul','george','ringo'],['guitar','bass','guitar',
'drums']}

then i need to put bands['beatles'][1][3]='percussion'

i presume... ?

Joe Francia <(E-Mail Removed)> wrote in message
news:JnWob.4104117$(E-Mail Removed)...
> ruari mactaggart wrote:
> > can i write
> >
> >>>>dictionary[key][list][3]

> >
> > to mean the 3rd item in the list that is the corresponding value for

'key' ?
>
> You don't need the[list] part. Assuming you mean something like:
>
> bands = {'beatles': ['john','paul','george','ringo']}
>
> ...then bands['beatles'][3] == 'ringo'
>
> Peace,
> Joe
>



 
Reply With Quote
 
Francis Avila
Guest
Posts: n/a
 
      11-02-2003

"ruari mactaggart" <(E-Mail Removed)> wrote in message
news:bo1ccs$juu$(E-Mail Removed)...
> what if i mean
>

bands={'beatles':['john','paul','george','ringo'],['guitar','bass','guitar',
> 'drums']}
>
> then i need to put bands['beatles'][1][3]='percussion'
>
> i presume... ?


If things are getting this complicated, you're probably better off a)
wrapping an interface to this dict using an object, or b) just using object
attributes and using those objects as dictionary items.

Oh, and you need to group those two lists into a single list or tuple....
--
Francis Avila

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
updating dictionaries from/to dictionaries Brandon Python 12 08-15-2008 12:35 AM
Pickling dictionaries containing dictionaries: failing,recursion-style! lysdexia Python 6 12-02-2007 12:03 AM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
naming convention for scalars, lists, dictionaries ... beliavsky@aol.com Python 8 03-01-2005 10:11 AM
Are lists at least as efficient as dictionaries? Narendra C. Tulpule Python 6 08-30-2003 08:20 PM



Advertisments