Stackoverflow - Python iteration matching