Priya Ramakrishnan
Priya Ramakrishnan

Reputation: 45

How to create multiple dictionaries from a single dictionary based on the almost same key values?

So, I am a newbie to Python. I need small help with programming in python. I have a dictionary as shown

dict = {'data1' : 50 , 'cache1' : 30, 'option1' : 90 , 
        'data2' : 45, 'cache2' : 67, 'option2' : 33,
         'data3': 56, 'cache3': 47, 'option3' : 25}

I have to create a 3 dictionaries as shown below:

dict1 = {'data1':50,'data2' : 45,'data3': 56}

dict2 = {'cache1' : 30,'cache2' : 67,'cache3':47}

dict3 = {'option1' : 90 ,'option2' : 33,'option3' :25}

Can anyone please help me with python to get this output.

Upvotes: 0

Views: 1260

Answers (5)

Zabir Al Nazi Nabil
Zabir Al Nazi Nabil

Reputation: 11218

A general solution for any number of cases:

mydict = {'data1' : 50 , 'cache1' : 30, 'option1' : 90 , 
        'data2' : 45, 'cache2' : 67, 'option2' : 33,
         'data3': 56, 'cache3': 47, 'option3' : 25}

nameset = set()  
for d in mydict.keys():
    nameset.add(''.join(a for a in d if a.isalpha()))

print(nameset)

all_dicts = {}

for n in nameset:
    all_dicts[n] = {}

for d in mydict.keys():
    for n in nameset:
        if n in d:
            all_dicts[n][d] = mydict[d]


print(all_dicts)

Out:

{'option', 'data', 'cache'}                                               
{'option': {'option1': 90, 'option3': 25, 'option2': 33}, 'data': {'data2'
: 45, 'data1': 50, 'data3': 56}, 'cache': {'cache2': 67, 'cache3': 47, 'ca
che1': 30}} 

Upvotes: 0

Skalex
Skalex

Reputation: 146

You could try this:

def Collect(column,dictionary):
    result = {}
    for key in dictionary:
        if column in key:
            result[key] = dictionary[key]
    return result

dict_ = {'data1' : 50 , 'cache1' : 30, 'option1' : 90 , 
        'data2' : 45, 'cache2' : 67, 'option2' : 33,
         'data3': 56, 'cache3': 47, 'option3' : 25}

dataDict = Collect("data",dict_)
cacheDict = Collect("cache",dict_)
optionDict = Collect("option",dict_)

print(dataDict)
print(cacheDict)
print(optionDict)

This will give you a result like the following:

enter image description here

Upvotes: 1

shaabhishek
shaabhishek

Reputation: 63

There are multiple ways you could do this. You can try:

dict1 = {key:value for key,value in dict.items() if 'data' in key}
dict2 = {key:value for key,value in dict.items() if 'cache' in key}
dict3 = {key:value for key,value in dict.items() if 'option' in key}

You could also do it in one go:

dict1, dict2, dict3 = {}, {}, {}
for key, value in dict.items():
  if 'data' in key:
    dict1[key] = value
  elif 'cache' in key:
    dict2[key] = value
  elif 'option' in key:
    dict3[key] = value

Upvotes: 1

Tom Ron
Tom Ron

Reputation: 6181

dict1 = {}
dict2 = {}
dict3 = {}

for k, v in dict.items():
    if k.startswith("data"):
       dict1[k] = v
    elif k.startswith("cache"):
       dict2[k] = v
    else:
       dict3[k] = v

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 117981

You could use a dict comprehension to create each of the desired dictionaries. I demonstrated data below, but you could use 'cache' and 'option' similarly.

>>> source = {'data1' : 50 , 'cache1' : 30, 'option1' : 90 ,
              'data2' : 45, 'cache2' : 67, 'option2' : 33,
              'data3': 56, 'cache3': 47, 'option3' : 25}
>>> {k: v for k, v in source.items() if 'data' in k}
{'data1': 50, 'data2': 45, 'data3': 56}

Upvotes: 1

Related Questions