From 8a841eb45736f9f54887ae7731730ce0fd0698cb Mon Sep 17 00:00:00 2001 From: Madhav Agarwal <136367379+madhavagarwal3012@users.noreply.github.com> Date: Sun, 31 Mar 2024 01:53:28 +0530 Subject: [PATCH] Update update method --- python/_set.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/python/_set.py b/python/_set.py index fc3bf33e..2ca645fa 100644 --- a/python/_set.py +++ b/python/_set.py @@ -16,8 +16,16 @@ class set: def clear(self): self._a.clear() - def update(self, other): - for elem in other: + def update(self, other): + if isinstance(other, set): + self._a.update({elem: None for elem in other}) + else: + try: + iterable = iter(other) + except TypeError: + raise TypeError("Input must be an iterable") + + for elem in iterable: self.add(elem) def __len__(self): @@ -77,4 +85,4 @@ class set: return '{'+ ', '.join([repr(i) for i in self._a.keys()]) + '}' def __iter__(self): - return iter(self._a.keys()) \ No newline at end of file + return iter(self._a.keys())