python - Trim an ordered dict to the last x items -
i'm trying trim ordered dict last x items.
i have following code, works doesn't seem pythonic.
is there better way of doing this?
import collections d = collections.ordereddict() # snip: populate dict here! d = collections.ordereddict(d.items()[-3:])
this works bit faster:
for k in range(len(d) - x) : data.popitem(last = false)
not sure how pythonic though.
benefits include not having cast create new ordereddict object, , not having @ keys or items.