diff --git a/rich/jupyter.py b/rich/jupyter.py index 24135a9f7..275fd2f96 100644 --- a/rich/jupyter.py +++ b/rich/jupyter.py @@ -26,10 +26,18 @@ def _repr_mimebundle_( self, include: Sequence[str], exclude: Sequence[str], **kwargs: Any ) -> Dict[str, str]: data = {"text/plain": self.text, "text/html": self.html} + + # Check if we need to filter by include if include: - data = {k: v for (k, v) in data.items() if k in include} + data = {k: data[k] for k in data if k in include} + # Early return if there's nothing to exclude + if not exclude: + return data + + # Check if we need to filter by exclude if exclude: - data = {k: v for (k, v) in data.items() if k not in exclude} + data = {k: data[k] for k in data if k not in exclude} + return data