list.index

Thomas Guettler guettli at thomas-guettler.de
Mon Jun 9 07:13:48 EDT 2003


On Fri, Jun 06, 2003 at 03:49:58PM +0200, delphiro wrote:
> First of all many, many thanks to the creators / maintainers of Python.
> 
> Since my discovery of this holy grail I have a development time for platform indepandent software which is almost identical to what is possible with Delphi for Windows. It is realy a great language and easily learned with my C / C++ and mostly Object Pascal experience. THANKS!
>  
> Now to the question...
> 
> is this the easiest way to check for the existance of an item in a list?
> it looks a bit weird to check a for a listitem by using a try..except block
> 
> (code background; if the item is found the colour should change else the colour that was already given is ok)
> [mycode]
> try:
>   x = self.selected_points.index( self.pointmarker )
>   colour = DEFAULT_SELECTED_POINT_COLOUR
> except ValueError:
>   pass
> [/mycode]

Hi!

For things like this I often use a dictionary with a list as value
(not tested code):

points={}

#add point:
old_value=points.get(mypoint)
if not old_value:
    old_value=[]
    points[mypoint]=old_values
old_values.append(mypoint)

#get points which are at mypoint:
mypoint_list=points.get(mypoint)

 thomas


-- 
Thomas Guettler <guettli at thomas-guettler.de>
http://www.thomas-guettler.de






More information about the Python-list mailing list