Consider the following dictionary, d:
d = {\'a\': 3, \'b\': 2, \'c\': 3, \'d\': 4, \'e\': 5}
I want to return the first N key:value pairs f
This might not be very elegant, but works for me:
d = {'a': 3, 'b': 2, 'c': 3, 'd': 4, 'e': 5} x= 0 for key, val in d.items(): if x == 2: break else: x += 1 # Do something with the first two key-value pairs