Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Split Dictionary values on size limit of values

Improve Article
Save Article
  • Last Updated : 27 Mar, 2021
Improve Article
Save Article

Given a dictionary with string values, the task is to write a python program to split values if the size of string exceeds K.

Input : {1 : “Geeksforgeeks”, 2 : “best for”, 3 : “all geeks”}, limit = 5

Output : {1: ‘Geeks’, 2: ‘forge’, 3: ‘eks’, 4: ‘best ‘, 5: ‘for’, 6: ‘all g’, 7: ‘eeks’}

Explanation : All string values are capped till length 5. New value is created post size limit.

Input : {1 : “Geeksforgeeks”, 2 : “best for”}, limit = 5

Output : {1: ‘Geeks’, 2: ‘forge’, 3: ‘eks’, 4: ‘best ‘, 5: ‘for’}

Explanation : All string values are capped till length 5. New value is created post size limit.

Method : Using dictionary comprehension + enumerate() + list slicing 

In this, we perform the task of getting required value chunks using list slicing and list comprehension on the iteration of values extracted using values(). The next step is to reassign keys with new chunked values using list comprehension and enumerate().

Python3




# Python3 code to demonstrate working of
# Split Dictionary values on size limit
# Using dictionary comprehension + enumerate() +  list slicing
  
# initializing dictionary
test_dict = {1: "Geeksforgeeks"
             2: "best for", 3
             "all geeks"}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing limit
limit = 4
  
# cutting chunks of K
chunks = (sub[idx: idx + limit] for sub in test_dict.values()
          for idx in range(0, len(sub), limit))
  
# re assigning dictionary with chunks
res = {key: val for key, val in enumerate(chunks, 1)}
  
# printing result
print("The extracted values : " + str(res))


Output:

The original dictionary is : {1: ‘Geeksforgeeks’, 2: ‘best for’, 3: ‘all geeks’}

The extracted values : {1: ‘Geek’, 2: ‘sfor’, 3: ‘geek’, 4: ‘s’, 5: ‘best’, 6: ‘ for’, 7: ‘all ‘, 8: ‘geek’, 9: ‘s’}

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!