Sets in Python

Michael Hudson mwh21 at cam.ac.uk
Tue Jan 30 12:26:18 EST 2001


"Magnus Lie Hetland" <mlh at idi.ntnu.no> writes:

> I quite often use dictionaries as sets in Python... But it doesn't really
> seem pretty enough to me... Some arbitrariness (as with
> "while 1: ... break" I still don't like that one...) about using what
> to me is a value as a key, and assigning it some value -- like 1,
> for instance... And then checking it with has_key, and removing
> it with del...
> 
> Isn't there a better (more beautiful) way of doing this?

This has gone round on python-dev in the last few weeks and the end
conclusion was "no".

> And if one was to write a small wrapper of some kind --
> is there any way to implement "x in s" without a linear search?
> Or does the "in" operator always loop from 0 and up?
> (It would certainly be nice to be able to override it...)

Tried __contains__?

Cheers,
M.

-- 
  I saw `cout' being shifted "Hello world" times to the left and
  stopped right there.                                -- Steve Gonedes



More information about the Python-list mailing list