Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
8-liner: simple solution in Clear category for Count Comprehensions by przemyslaw.daniel
from typing import Dict
import ast
def count_comprehensions(code: str) -> Dict[str, int]:
types = "GeneratorExp", "ListComp", "SetComp", "DictComp"
parse = ast.dump(ast.parse(code))
return {tp: ps for tp in types if (ps:=parse.count(tp))}
Dec. 12, 2020