Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split a dictionary in several dictionaries based on a part of values using Python

Tags:

People also ask

How do you break nested dictionaries in python?

To delete an item stored in a nested dictionary, we can use the del statement. The del statement lets you delete an object. del is written like a Python break statement , on its own line, followed by the item in the dictionary that you want to delete.

Can we slice dictionary in python?

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.

Can dictionaries be values in other dictionaries?

But the key values of the dictionary contain different types of values that don't need to maintain any order. When one or more dictionaries are declared inside another dictionary then it is called a nested dictionary or dictionaries of the dictionary.


I have a dictionary like the following

dict={'sku1':'k-1','sku2':'k-2','sku3':'b-10','sku4':'b-1','sku5':'x-1', 'sku6':'x-2'}

How can I get separate dictionaries based on first letter of value? like:

dict1={'sku5':'x-1','sku6':'x-2'}
dict2={'sku1':'k-1','sku2':'k-2'}
dict2={'sku3':'b-10','sku4':'b-1'}

I started with

for an_item in thevalues_ofdict:
    Splitted_Pcodes.append(int(an_item.split("-")[0]))
    for i in Splitted_Pcodes:
        if i=='x':

but how I will find the whole initial dicitonary element