On Fri, 24 Apr 2009 03:22:50 -0700, Paul Rubin wrote: > Steven D'Aprano <st...@remove-this-cybersource.com.au> writes: >> len() works on dicts and sets, and they're not sequences. > > Of course dicts and sets are sequences.
Dicts and sets are explicitly described as "other containers": http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode- list-tuple-buffer-xrange Note that addition is included in the table of sequence operations, but neither dicts nor sets support it: >>> {} + {} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'dict' and 'dict' Nor do they support slicing. > But there are also sequences on which len doesn't work. > > You could use: sum(1 for x in seq) > Of course iterating through seq may have undesired side effects. It's also not guaranteed to terminate. Well, technically no user-defined function is guaranteed to terminate, but you know what I mean :) -- Steven -- http://mail.python.org/mailman/listinfo/python-list