user12341055
user12341055

Reputation:

Make List of Dictionary as a value for key of Dictionary

I have following List of Dictionaries.

p = [ {"Id": "47","Name": "l","Cate": "GD31","Mark": 0,"Code": "l"},
     {"Id": "58","Name": "k","Cate": "GD33","Mark": 0,"Code": "k"},
    {"Id": "40","Name": "j","Cate": "GD35","Mark": 0,"Code": "j"},
    {"Id": "15","Name": "i","Cate": "GD37","Mark": 0,"Code": "i"},
    {"Id": "39","Name": "h","Cate": "GD39","Mark": 0,"Code": "h"},
    {"Id": "75","Name": "g","Cate": "LD31","Mark": 0,"Code": "g"},
    {"Id": "12","Name": "f","Cate": "LD33","Mark": 0,"Code": "d"},
    {"Id": "92","Name": "e","Cate": "LD35","Mark": 0,"Code": "e"},
    {"Id": "42","Name": "d","Cate": "LD37","Mark": 0,"Code": "d"},
    {"Id": "11","Name": "c","Cate": "LD39","Mark": 0,"Code": "c"},
    {"Id": "51","Name": "b","Cate": "SWW","Mark": 0,"Code": "b"},
    {"Id": "13","Name": "a","Cate": "SMM","Mark": 0,"Code": "a"} ]

In the above dictionary, I want to store the particular dictionary to some variable based on the "Cate" value equal to GD31 or LD31. What I have tried is:

if any(d['Cate'] == 'GD31' or d['Cate'] == 'LD31'  for d in p):
     print('D31 present')

In the above code, I Only check whether GD31 or LD31 is present or not. I need to store the particular dictionary (Where "Cate" value equal to GD31 or LD31) in some variable and also update that selected dictionaries like following:

new = [ { 'GD31':[{"Id":"47","Name_G":'l'}],
          'LD31':[{"Id":"75","Name_L":'g'}] }  ]

Explanation: If both GD31 and LD31 are present, then make GD31 and LD31 as a key and "Id, Name" as values then update "Name" to "Name_G" for GD31 and "Name" to "Name_L" for LD31.

I Expect the Result as:

       new = [ { 'GD31':[{"Id":"47","Name_G":'l'}],
               'LD31':[{"Id":"75","Name_L":'g'}] }  ]

Explanation:

If D31 is found, Select that particular Dictionary Alone from the list of dictionaries. In that particular Dictionary Take "Id" and "Name". Hence, GD31 and LD31 is a key and "Id, Name" are values. And If D31 is found, update "Name" as "Name_G" for GD31 and "Name_L" for LD31. ***And this is for all values D33, D35, D37 and D39

I hope you all understand. Any Idea Please?

Upvotes: 2

Views: 105

Answers (2)

oppressionslayer
oppressionslayer

Reputation: 7204

You can use a function so you can gather any gate you want with with a call to it:

p = [{"Id": "47","Name": "l","Cate": "GD31","Mark": 0,"Code": "l"},
    {"Id": "58","Name": "k","Cate": "GD33","Mark": 0,"Code": "k"},
    {"Id": "40","Name": "j","Cate": "GD35","Mark": 0,"Code": "j"},
    {"Id": "15","Name": "i","Cate": "GD37","Mark": 0,"Code": "i"},
    {"Id": "39","Name": "h","Cate": "GD39","Mark": 0,"Code": "h"},
    {"Id": "75","Name": "g","Cate": "LD31","Mark": 0,"Code": "g"},
    {"Id": "12","Name": "f","Cate": "LD33","Mark": 0,"Code": "d"},
    {"Id": "92","Name": "e","Cate": "LD35","Mark": 0,"Code": "e"},
    {"Id": "42","Name": "d","Cate": "LD37","Mark": 0,"Code": "d"},
    {"Id": "11","Name": "c","Cate": "LD39","Mark": 0,"Code": "c"},
    {"Id": "51","Name": "b","Cate": "SWW","Mark": 0,"Code": "b"},
    {"Id": "13","Name": "a","Cate": "SMM","Mark": 0,"Code": "a"} ]

def getgates(hm): 
  s={} 
  for item in p:  
     if hm in item['Cate']:  
        s[item['Cate']] = [{"Id": item['Id'], "Name_" + item['Cate'][0]: item['Code']}]  
  return s 



print(getgates('D31'))
{'LD31': [{'Id': '75', 'Name_L': 'g'}], 'GD31': [{'Id': '47', 'Name_G': 'l'}]}

print(getgates('D35'))
{'LD35': [{'Id': '92', 'Name_L': 'e'}], 'GD35': [{'Id': '40', 'Name_G': 'j'}]}

print(getgates('D39'))
{'LD39': [{'Id': '11', 'Name_L': 'c'}], 'GD39': [{'Id': '39', 'Name_G': 'h'}]}

Upvotes: 0

iz_
iz_

Reputation: 16593

Try this:

p = [{"Id": "47","Name": "l","Cate": "GD31","Mark": 0,"Code": "l"},
    {"Id": "58","Name": "k","Cate": "GD33","Mark": 0,"Code": "k"},
    {"Id": "40","Name": "j","Cate": "GD35","Mark": 0,"Code": "j"},
    {"Id": "15","Name": "i","Cate": "GD37","Mark": 0,"Code": "i"},
    {"Id": "39","Name": "h","Cate": "GD39","Mark": 0,"Code": "h"},
    {"Id": "75","Name": "g","Cate": "LD31","Mark": 0,"Code": "g"},
    {"Id": "12","Name": "f","Cate": "LD33","Mark": 0,"Code": "d"},
    {"Id": "92","Name": "e","Cate": "LD35","Mark": 0,"Code": "e"},
    {"Id": "42","Name": "d","Cate": "LD37","Mark": 0,"Code": "d"},
    {"Id": "11","Name": "c","Cate": "LD39","Mark": 0,"Code": "c"},
    {"Id": "51","Name": "b","Cate": "SWW","Mark": 0,"Code": "b"},
    {"Id": "13","Name": "a","Cate": "SMM","Mark": 0,"Code": "a"} ]

new = [{d["Cate"]: [{"Id": d["Id"], "Name_" + d["Cate"][0]: d["Name"]}] for d in p if "D31" in d["Cate"]}]

print(new)

Output:

[{'GD31': [{'Id': '47', 'Name_G': 'l'}], 'LD31': [{'Id': '75', 'Name_L': 'g'}]}]

Upvotes: 1

Related Questions