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
For Python 3.8 the correct answer should be:
import more_itertools d = {'a': 3, 'b': 2, 'c': 3, 'd': 4, 'e': 5} first_n = more_itertools.take(3, d.items()) print(len(first_n)) print(first_n)
Whose output is:
3 [('a', 3), ('b', 2), ('c', 3)]
After pip install more-itertools of course.
pip install more-itertools