In Python how to obtain a partial view of a dict?

dictionary, python, python-2.7

Solution

Kinda strange desire, but you can get that by using this

from itertools import islice

# Python 2.x
dict(islice(mydict.iteritems(), 0, 2))

# Python 3.x
dict(islice(mydict.items(), 0, 2))

or for short dictionaries

# Python 2.x
dict(mydict.items()[0:2])

# Python 3.x
dict(list(mydict.items())[0:2])

Problem

Is it possible to get a partial view of a `dict` in Python analogous of pandas `df.tail()/df.head()`. Say you have a very long `dict`, and you just want to check some of the elements (the beginning, the end, etc) of the `dict`. Something like: ``` dict.head(3) # To see the first 3 elements of the dictionary. {[1,2], [2, 3], [3, 4]} ``` Thanks

Original source