Iterating over a dictionary's sorted keys

Peter Schneider-Kamp petersc at stud.ntnu.no
Mon May 29 10:46:44 CEST 2000


Mark Hammond wrote:
> 
> I agree its handy, but dont think it's worth adding a convenience function
> to collapse two lines down to one.  There are _lots_ of really, really
> handy things you can do in 2 lines of Python, so where would we stop?

When we reach the complexity of the Java interface...

But he can always take a copy of UserDict.py with his program an put
his convenience stuff in there. As long as he distributes myDict.py
it's imho no problem.

Java-'s-not-that-bad-but-don't-let-your-children-near-it-ly y'rs
Peter

*** UserDict.py Mon May 29 10:41:25 2000
--- myDict.py   Mon May 29 10:39:19 2000
***************
*** 21,26 ****
--- 21,30 ----
          import copy
          return copy.copy(self)
      def keys(self): return self.data.keys()
+     def sorted_keys(self):
+         keys = self.data.keys()
+         keys.sort()
+         return keys
      def items(self): return self.data.items()
      def values(self): return self.data.values()
      def has_key(self, key): return self.data.has_key(key)




More information about the Python-list mailing list