From f5afb7f2331efa8f64080576a75517c3a96442b9 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 31 Oct 2022 23:44:40 -0500 Subject: [PATCH] GH-98766: Modest speed-up from ChainMap.__iter__ (GH-98946) --- Lib/collections/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 58607874be9..f07ee143a5a 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -1011,8 +1011,8 @@ class ChainMap(_collections_abc.MutableMapping): def __iter__(self): d = {} - for mapping in reversed(self.maps): - d.update(dict.fromkeys(mapping)) # reuses stored hash values if possible + for mapping in map(dict.fromkeys, reversed(self.maps)): + d |= mapping # reuses stored hash values if possible return iter(d) def __contains__(self, key):