Python | Get first K items in dictionary
While working with dictionaries, we can come across a problem in which we might have to get just some of the initial keys in dictionary. This problem can typically occur in cases of web development domain. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using items() + list slicing
To solve this problem, combination of above functions have to implied. The items function can be used to get all the dictionary items and main task is done by list slicing, which limits the dictionary key-value pair.
# Python3 code to demonstrate working of # Get first K items in dictionary # Using items() + list slicing # Initialize dictionary test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5} # printing original dictionary print("The original dictionary : " + str(test_dict)) # Initialize limit K = 3 # Using items() + list slicing # Get first K items in dictionary res = dict(list(test_dict.items())[0: K]) # printing result print("Dictionary limited by K is : " + str(res)) |
The original dictionary : {‘is’: 2, ‘CS’: 5, ‘best’: 3, ‘gfg’: 1, ‘for’: 4}
Dictionary limited by K is : {‘is’: 2, ‘CS’: 5, ‘best’: 3}
Method #2 : Using islice() + items()
The combination of above functions can be used to perform this particular task. In these, we perform the slice using the islice() and items function allows to get the items out of iterable.
# Python3 code to demonstrate working of # Get first K items in dictionary # Using islice() + items() import itertools # Initialize dictionary test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5} # printing original dictionary print("The original dictionary : " + str(test_dict)) # Initialize limit K = 3 # Using islice() + items() # Get first K items in dictionary res = dict(itertools.islice(test_dict.items(), K)) # printing result print("Dictionary limited by K is : " + str(res)) |
The original dictionary : {‘is’: 2, ‘CS’: 5, ‘best’: 3, ‘gfg’: 1, ‘for’: 4}
Dictionary limited by K is : {‘is’: 2, ‘CS’: 5, ‘best’: 3}
Recommended Posts:
- Python Dictionary | items() method
- Dictionary Methods in Python | Set 1 (cmp(), len(), items()...)
- Python | Sort the items alphabetically from given dictionary
- Python | Delete items from dictionary while iterating
- Python | Type conversion of dictionary items
- Python | Get items in sorted order from given dictionary
- Python program to find the sum of all items in a dictionary
- Python | Count number of items in a dictionary value that is a list
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python | Remove items from Set
- Python | Pandas Series.items()
- Python | Common items among dictionaries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



