Skip to content

Set-item is not overwriting the existing key #1

Description

@vasanth62

Hi Rob,

I came across this repo via stackoverflow. I was trying to use it, however noticed an issue where the existing keys are not overwritten. The below diff fixes it.

*************** class RandomDict(MutableMapping):
*** 20,30 ****
      def __setitem__(self, key, val):
          if key in self.keys:
              i = self.keys[key]
          else:
              self.last_index += 1
              i = self.last_index

-         self.values.append((key, val))
          self.keys[key] = i

      def __delitem__(self, key):
--- 28,39 ----
      def __setitem__(self, key, val):
          if key in self.keys:
              i = self.keys[key]
+             self.values[i] = (key, val)
          else:
              self.last_index += 1
              i = self.last_index
+             self.values.append((key, val))

          self.keys[key] = i

      def __delitem__(self, key):

Edit: Formatting

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions