© Joannah Nanjekye 2017
Joannah NanjekyePython 2 and 3 Compatibilityhttps://doi.org/10.1007/978-1-4842-2955-2_10

10. Collections and Iterators

Joannah Nanjekye
Kampala, Uganda
Python 3 is known to avoid returning lists and dictionaries. If k is a dictionary, in Python 2, k.keys() returns a list of the keys in the dictionary, while in Python 3, k.keys() enables iteration over the keys in something like a for loop. Similarly, k.values() and k.items() returns lists of values or key-value pairs in Python 2, while in Python 3 we can only iterate over the values in a for loop. Python 3 only contains range and it behaves like the Python 2’s xrange and in Python 3, the built-in map behaves like Python 2’s itertools.imap. Therefore in Python 2 and 3, ...

Get Python 2 and 3 Compatibility: With Six and Python-Future Libraries now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.