Alex alex at somewhere.round.here
Sat Mar 18 17:49:00 CET 2000

> It would be nice to have some more bulletproof way than the only other
> approach I can think of:

See Tim Peters' plumbo.py for something more complete along these lines:

from types import ListType

def IsRecursiveList(x):
    element_ids = {}
    element_ids[id (x)] = None
    for element in x:
        if type (element) == ListType:
            element_id = id (element)
            if element_ids.has_key (element_id):
                return 1
                element_ids[element_id] = None
        return 0

if __name__ == '__main__':
    a = []
    a.append (a)
    assert IsRecursiveList (a)
    assert not IsRecursiveList ([])            


More information about the Python-list mailing list