I would say it is better to use the future
module than to implement your own, many things are already done for you in a minimalistic / optimized way:
from future.utils import viewitems foo = [key for key, value in viewitems(some_dict) if value.get('marked')]
if you are wondering how this viewitems()
works, it's that simple:
def viewitems(obj, **kwargs): """ Function for iterating over dictionary items with the same set-like behaviour on Py2.7 as on Py3. Passes kwargs to method.""" func = getattr(obj, "viewitems", None) if not func: func = obj.items return func(**kwargs)
Note: if compatibility with Python versions prior to 2.7 is necessary to use iteritems()
:
from future.utils import iteritems foo = [key for key, value in iteritems(some_dict) if value.get('marked')]
source share