Are Python deques linked lists?

Peter Otten __peter__ at web.de
Mon Dec 10 12:09:22 EST 2007


Neil Cerutti wrote:

> On 2007-12-10, Duncan Booth <duncan.booth at invalid.invalid> wrote:

>> def test():
>>     ll = LinkedList([random.randint(1,1000) for i in range(10)])
>>
>>     for el in ll:
>>         if el.value%2==0:
>>             ll.delete(el)
>>
>>     print [el.value for el in ll]
>>
>>
>> if __name__=='__main__':
>>     test()
>>
>> Support for deleting elements other than the current one, and
>> insertBefore/insertAfter methods is left as an exercise.
> 
> Making an object its own iterator [works] for files, but not for a
> container. After the deletions, you can never iterate again.

Look at the test code again -- there is a second iteration after the
deletions (the list comprehension). 

However, you will get into trouble if you try to run two simultaneous
iterations over the same LinkedList, so there is room for another 
exercise ;)

Peter



More information about the Python-list mailing list