Data structure question

Ned Batchelder ned at nedbatchelder.com
Mon Nov 18 03:24:09 CET 2013


On Sunday, November 17, 2013 9:03:38 PM UTC-5, Joseph L. Casale wrote:
> I have a need for a script to hold several tuples with three values, two text
> strings and a lambda. I need to index the tuple based on either of the two
> strings. Normally a database would be ideal but for a self-contained script
> that's a bit much.
> 
> Before I re-invent the wheel, are there any built-in structures that allow for
> this type of use case?
> 
> Thanks,
> jlc

Sounds like you should create a dictionary, and enter each tuple twice, once with each string as a key:

    d = {}
    for t in the_tuples:
        d[t[0]] = d[t[1]] = t

--Ned.



More information about the Python-list mailing list