from typing import List, Dict, Any def list_remove_all_occurrences(l: List[Any], value: Any) -> None: """ Remove all occurrences of a given value from a list **in place** See https://stackoverflow.com/a/48253792 """ i = -1 # Shift elements that should not be removed for i, v in enumerate(filter(value.__ne__, l)): l[i] = v # Remove the tail del l[i + 1: len(l)] def check_list_element_no_bounds(l: List[Any], index: int, expected_value: Any) -> bool: """ Check if l[index] is equal to expected_value, but without raising an exception if l does not have that many elements """ try: return l[index] == expected_value except IndexError: return False def check_dict_element_no_exception(d: Dict[Any, Any], key: Any, expected_value: Any) -> bool: """ Check if d[key] is equal to expected_value, but without raising an exception if d does not contain the key """ try: return d[key] == expected_value except KeyError: return False