Use iterators for unique and len in python backend