Handling 2.7 and 3.0 Versions of Dict

Terry Reedy tjreedy at udel.edu
Wed Aug 31 05:33:37 CEST 2011

On 8/30/2011 9:43 PM, Travis Parks wrote:
> I am writing a simple algorithms library that I want to work for both
> Python 2.7 and 3.x. I am writing some functions like distinct, which
> work with dictionaries under the hood. The problem I ran into is that
> I am calling itervalues or values depending on which version of the
> language I am working in. Here is the code I wrote to overcome it:
> import sys
> def getDictValuesFoo():
>      if sys.version_info<  (3,):
>          return dict.itervalues
>      else:
>          return dict.values

One alternative is to use itervalues and have 2to3 translate for you.
Terry Jan Reedy

More information about the Python-list mailing list