Interface of the set classes

Steven Bethard steven.bethard at gmail.com
Fri Oct 29 19:37:19 CEST 2004


Pierre Barbier de Reuille <pierre.barbier <at> cirad.fr> writes:
> 
> I don't _need_ __getitem__ for itself. I need it because I don't know 
> how to iterate over a list and store the current position for future 
> use.

I'm not sure I understand what you're trying to do here.  Could you give an
example?  Iterators are resumable, so at least one sense of "storing the current
position" is just retaining the iterator:

>>> l = [11, 1, 2, 2, 3, 5, 11, 3, 7, 5]
>>> items = iter(set(l))
>>> for i, item in enumerate(items):
... 	print item
... 	if i == 3:
... 		break
... 	
1
2
3
5
>>> for item in items:
... 	print item
... 	
7
11

If you could post an example of what you're doing right now with lst[x], maybe
we could suggest some other ways of approaching it that take advantage of, say
__iter__, instead of __getitem__?

Steve




More information about the Python-list mailing list