add indexes on the fly to lists

Diez B. Roggisch deetsNOSPAM at
Mon Jan 24 22:33:27 CET 2005

The data structure you want to use is a dictionary - known as hashmap or
hash or map in other languages. Lets say you have a few objects of type
town that you want to access using their zip, then things could look like

class Town(object):
    def __init__(self, zip, cool_clubs): = zip
        self.colo_clubs = cool_clubs

towns = [Berlin(10439, ["Marietta", "ICON", "Bastart"]),
         Cologne(50000, ["Subway", "Stadtgarten", "Gebäude 9"])]

index = {}
for town in towns:
     index[] = town

Hope that makes it clear to you - look into the tutorial for a chapter about
python data structures.


Diez B. Roggisch

More information about the Python-list mailing list