When to use iteritems() vs items() in Python 3?

Ah, yes, if you’ve ever had to work across both Python versions, this can get a bit tricky. When you’re dealing with older codebases or need compatibility between Python 2 and 3, wrapping the .items() and .iteritems() in a try-except block is a smart move. I usually do it like this:

try:
    # Python 3
    items = dict.items()
except AttributeError:
    # Python 2
    items = dict.iteritems()

for key, value in items:
    print(key, value)

With this, you’re set regardless of whether the code runs in Python 2 or Python 3. It ensures that you’re always using the right method based on the Python version. But remember, in Python 3, you’re already getting lazy iteration with .items(), so there’s no need to worry about iteritems() Python 3-specific details anymore. This is how I keep things neat and maintain compatibility when working with mixed environments.