Skip to content
Discussion options

You must be logged in to vote

Micropython is developed for Microcontrollers and must be very memory efficient. The reversed method is not implemented for OrderedDict nor for dict.

Possible inefficient solution:

from collections import OrderedDict


class ItemsView:
    """
    ItemsView to support reversed
    But this implementation is memory-inefficient
    """
    def __init__(self, parent):
        self.parent = parent
    
    def __reversed__(self):
        for key in reversed(list(self.parent)):
            yield key, self.parent[key]


class TaskDict(OrderedDict):
    def items(self):
        return ItemsView(self)


print(list(reversed(TaskDict(a=10, b=20, c=30).items())))

It's simpler to use a list for task…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@jimmo
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by mishal
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants