from collections.abc import Iterator from itertools import tee from typing import Tuple, TypeVar, Collection, Any, List def hasNext(it)->Tuple[bool, Iterator]: ''' A support function to test hasNext for standard python iterators. usage, assuming it is already created (eg it=iter([1,2,3]): nx, it = hasNext(it) After the call, nx will be True iff it has a next value. The new it will be in original state (we have to damage the original iterator) ''' it,it1=tee(it) try: next(it1) return True, it except: return False, it T=TypeVar("T") def remove(lst:List[T], it: Any) -> bool: ''' remove all occurences of it from lst @param lst a list to remove items from @param it an item to remove @return True if lst was modified ''' n=lst.count(it) for i in range(n): lst.remove(it) return n>0 def removeAll(lst:List[T], toremove:Collection[Any]) -> bool: ''' all items in toremove are removed from lst @param lst a list to remove items from @param toremove a list of items to remove @return True if lst was modified ''' return any([remove(lst,it) for it in toremove])