From e8680887071e44aea63d9074cbdefa6139fe0eb6 Mon Sep 17 00:00:00 2001 From: geographybuff <31372572+geographybuff@users.noreply.github.com> Date: Wed, 7 May 2025 02:39:18 -0700 Subject: [PATCH] Smooth out redundant lists Removes unnecessary step that created nested lists, only for them to be flattened in the very next step, when the fully flattened lists can instead be instantiated in a single step, thus saving (subject to testing) perhaps 20% of the time on the entire .union() operation --- ordered_set/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ordered_set/__init__.py b/ordered_set/__init__.py index ccd1cbf..72caedd 100644 --- a/ordered_set/__init__.py +++ b/ordered_set/__init__.py @@ -365,8 +365,7 @@ def union(self, *sets: SetLike[T]) -> "OrderedSet[T]": cls: type = OrderedSet if isinstance(self, OrderedSet): cls = self.__class__ - containers = map(list, it.chain([self], sets)) - items = it.chain.from_iterable(containers) + items = itertools.chain(self, *sets) return cls(items) def __and__(self, other: SetLike[T]) -> "OrderedSet[T]":