Are Python deques linked lists?

Neil Cerutti horpner at yahoo.com
Mon Dec 10 09:31:37 EST 2007


On 2007-12-10, Peter Otten <__peter__ at web.de> 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



More information about the Python-list mailing list