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.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo