dictionaries with nested lists

Discussion in 'Python' started by Alex Martelli, Nov 1, 2003.

  1. ruari mactaggart wrote:

    > can i write
    >>>>dictionary[key]
    • [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
      • notation is unneeded and unsupported,
        -- indices start from 0 so the 3rd one is indexed as [2]


        Alex
     
    Alex Martelli, Nov 1, 2003
    #1
    1. Advertising

  2. Alex Martelli

    Joe Francia Guest

    ruari mactaggart wrote:
    > can i write
    >
    >>>>dictionary[key]
    • [3]

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


    • You don't need the
      • part. Assuming you mean something like:

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

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

        Peace,
        Joe
     
    Joe Francia, Nov 1, 2003
    #2
    1. Advertising

  3. 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
     
    Alex Martelli, Nov 1, 2003
    #3
  4. [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
     
    Raymond Hettinger, Nov 1, 2003
    #4
  5. can i write
    >>>dictionary[key]
    • [3]


    • to mean the 3rd item in the list that is the corresponding value for 'key' ?
     
    ruari mactaggart, Nov 2, 2003
    #5
  6. 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 <> wrote in message
    news:JnWob.4104117$...
    > ruari mactaggart wrote:
    > > can i write
    > >
    > >>>>dictionary[key]
    • [3]

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

    • 'key' ?
      >
      > You don't need the
      • part. Assuming you mean something like:
        >
        > bands = {'beatles': ['john','paul','george','ringo']}
        >
        > ...then bands['beatles'][3] == 'ringo'
        >
        > Peace,
        > Joe
        >
     
    ruari mactaggart, Nov 2, 2003
    #6
  7. "ruari mactaggart" <> wrote in message
    news:bo1ccs$juu$...
    > 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
     
    Francis Avila, Nov 2, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Narendra C. Tulpule

    Are lists at least as efficient as dictionaries?

    Narendra C. Tulpule, Aug 29, 2003, in forum: Python
    Replies:
    6
    Views:
    326
    Chad Netzer
    Aug 30, 2003
  2. Replies:
    8
    Views:
    327
    Paul Boddie
    Mar 1, 2005
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    410
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. lysdexia
    Replies:
    6
    Views:
    508
    John Machin
    Dec 2, 2007
  5. Brandon
    Replies:
    12
    Views:
    492
    Brandon
    Aug 15, 2008
Loading...

Share This Page