You could try this: print t, x[t]
Or this:
for key, val in x.items(): if key > 2: print key, val
If you want to get only the pairs that you're printing into a new dictionary, then you have to put them there:
newdict = {} for key, val in x.items(): if key > 2: print key, val # put all the keys > 2 into the new dict newdict[key] = val
Of course that solution is printing when it inserts. If you're doing more than just a little script you will get more utility out of breaking functionality out, making sure that any given function does one, and only one, thing:
def filter_dict(x): newdict = {} for key, val in x.items(): if key > 2: # put all the keys > 2 into the new dict newdict[key] = val def pretty_print(dct): for k, v in dct.items(): print k, v filtered = filter_dict(x) pretty_print(dct)
This doesn't apply if you're just debugging, of course, and there are ways in which it depends on how large of a program you're writing: if you're doing a simple enough thing then the extra flexibility you get by breaking functionality up is lost because of the extra effort of figuring out exactly what any given thing does. That said: you should generally do it.
Additionally, the most idiomatic way to filter lists on a simple comparison in Python is to use a dict comprehension (new in Python 2.7/3.0):
filtered = {key: val for key, val in x if key > 2}