Detect loops in lists