Given dictionary with value as lists, slice each list till K.
Input : test_dict = {“Gfg” : [1, 6, 3, 5, 7], “Best” : [5, 4, 2, 8, 9], “is” : [4, 6, 8, 4, 2]}, K = 3
Output : {‘Gfg’: [1, 6, 3], ‘Best’: [5, 4, 2], ‘is’: [4, 6, 8]}
Explanation : The extracted 3 length dictionary value list.Input : test_dict = {“Gfg” : [1, 6, 3, 5, 7], “Best” : [5, 4, 2, 8, 9], “is” : [4, 6, 8, 4, 2]}, K = 2
Output : {‘Gfg’: [1, 6], ‘Best’: [5, 4], ‘is’: [4, 6]}
Explanation : The extracted 2 length dictionary value list.
Method #1 : Using loop + list slicing
The combination of above functionalities can be used to solve this problem. In this, we perform task of list slicing using slice operation and loop to iterate through all the keys.
Python3
# Python3 code to demonstrate working of # Slice till K dictionary value lists # Using loop + list slicing # initializing dictionary test_dict = {"Gfg" : [1, 6, 3, 5, 7], "Best" : [5, 4, 2, 8, 9], "is" : [4, 6, 8, 4, 2]} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # initializing K K = 4 res = dict() for sub in test_dict: # slicing till K and reassigning res[sub] = test_dict[sub][:K] # printing result print("The dictionary after conversion " + str(res)) |
The original dictionary is : {‘Gfg’: [1, 6, 3, 5, 7], ‘Best’: [5, 4, 2, 8, 9], ‘is’: [4, 6, 8, 4, 2]}
The dictionary after conversion {‘Gfg’: [1, 6, 3, 5], ‘Best’: [5, 4, 2, 8], ‘is’: [4, 6, 8, 4]}
Method #2 : Using dictionary comprehension + list slicing
This is yet another way in which this task can be performed. In this, we perform task of reassigning using dictionary comprehension and provides one liner approach to this problem.
Python3
# Python3 code to demonstrate working of # Slice till K dictionary value lists # Using dictionary comprehension + list slicing # initializing dictionary test_dict = {"Gfg" : [1, 6, 3, 5, 7], "Best" : [5, 4, 2, 8, 9], "is" : [4, 6, 8, 4, 2]} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # initializing K K = 4 # one-liner to slice elements of list # items() used to get all key-value pair of dictionary res = {key: val[:K] for key, val in test_dict.items()} # printing result print("The dictionary after conversion " + str(res)) |
The original dictionary is : {‘Gfg’: [1, 6, 3, 5, 7], ‘Best’: [5, 4, 2, 8, 9], ‘is’: [4, 6, 8, 4, 2]}
The dictionary after conversion {‘Gfg’: [1, 6, 3, 5], ‘Best’: [5, 4, 2, 8], ‘is’: [4, 6, 8, 4]}
Recommended Posts:
- Python - Convert Lists into Similar key value lists
- Python | Convert list of tuples to dictionary value lists
- Python | Iterate through value lists dictionary
- Python | Concatenate dictionary value lists
- Python - Cumulative product of dictionary value lists
- Python - Convert Key-Value list Dictionary to List of Lists
- Python - Filter odd elements from value lists in dictionary
- Python - Unique value keys in a dictionary with lists as values
- Python - Reinitialize Value lists to K in Dictionary
- Python - Convert Strings to Uppercase in Dictionary value lists
- Python - Dictionary Key Value lists combinations
- Python - Cross mapping of Two dictionary value lists
- Python - Dictionary value lists lengths product
- Python | Pretty Print a dictionary with dictionary value
- Python - Convert Dictionary Value list to Dictionary List
- Python - Replace dictionary value from other dictionary
- Python | Prefix Sum Subarray till False value
- Python - Maximum element till K value
- Python | Multiplication till Null value
- Python - Elements Product till K value
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.

