On 2007-12-10, Peter Otten <[EMAIL PROTECTED]> wrote: > Neil Cerutti wrote: >> [linked lists] don't work well with Python iterators, which >> aren't suitable for a linked list's purposes--so you have to >> give up the happy-joy for loop syntax in favor of Python's >> frowny-sad while loops. > > You can always move the while-loop into a generator and use > for-loops happily ever after.
Python's iterators are unsuitable for mutating the linked list while iterating--the only major application of linked lists. Wrapping in a generator won't allow you to use for loop syntax, unless I'm missing something, which has often happened. # x is a linked list object, containing random numbers. # delete even numbers x_iter = x.begin() while x_iter != x.end(): if x_iter.value % 2 == 0: x_iter = x.delete(x_iter) # or x_iter.delete() as an iter mutating op? else: x_iter.advance() Of course, a linked lists type would be obliged to provide a filter method instead. C++ "solved" this difficulty by making all containers equally awkward to work with. ;-) -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list