[Zope] referencing tuples
Thomas B. Passin
tpassin@mitretek.org
Tue, 23 Apr 2002 10:40:56 -0400
Have your method return a dictionary instead of a list of tuples. Then you
can directly access an item by its key. A tuple is not indexed except by
position, so you can't jump into the middle by using a key.
It's probably a non-issue, though. The length of your list would have to be
pretty large for there to be a noticable delay because you have to look
through the list. Still, a dictionary is what you want.
Cheers,
Tom P
[Dianne Van Dulken]
>
> I was wondering if what I am trying to do is possible, or not.
>
> I have a method that returns a list of tuple pairs eg:
>
> [(firstname, Di), (lastname, van Dulken), (address, ...) ] etc.
>
> I have no problem looping through this to get the sequence-item,
> sequence-key information:
>
> <dtml-in "getDetails(_.None, _, status='Active')">
> <dtml-in sequence-item>
> <dtml-var sequence-key> = <dtml-var sequence-item>
> </dtml-in>
> <hr>
> </dtml-in>
>
> works fine.
>
> BUT, I really only want (on this page, the method is used on other pages)
to
> know a couple of the tuple details in the list.
> I know I can get this by looping through the whole sequence and using
>
> <dtml-if "sequence-key == 'firstname'">
>
> but that seems an incredibly slow and clunky way of referencing it to me.
> Is there anyway I can reference the tuple value by the tuple key without
> having to loop through them that way? I've tried getitem('firstname') and
> sequence-var-firstname, but neither of these work. I couldn't find any
one
> else asking this, so I assume it is too basic for normal people to query
on.
>
> I also know I can reference THE tuple I am after by using sequence-item[1]
> (or whatever), but that rather removes the whole point of having name
> variables.