Converting python dictionary value into nested keys

Question:

I have the following dictionary:

{'expert_interests': "['Machine learning', 'deep learning']"}

As you can see the the value of the key "expert_interests" is the String "[‘Machine learning’, ‘deep learning’]".

I want to convert this string value into a nested dictionary such that the dictionary looks like this:

{'expert_interests': [
      {
        "title": "Machine learning"
      },
      {
        "title": "deep learning"
        
      }
      
    ]
    }

I do not know how to approach this problem.Any help is highly appreciated

Asked By: SIM777

||

Answers:

You can use ast.literal_eval to convert the value to a list of strings and use a list comprehension to create a list of dicts from it.

from ast import literal_eval
d = {'expert_interests': "['Machine learning', 'deep learning']"}
d['expert_interests'] = [{"title" : t} for t in literal_eval(d['expert_interests'])]
print(d) 
# {'expert_interests': [{'title': 'Machine learning'}, {'title': 'deep learning'}]}
Answered By: Unmitigated

The best option is to use ast.literal_eval. This function is like eval, except it will only evaluate literals, so it’s safer from attacks.

From the documentation (linked above),

There is no namespace, no name lookups, or ability to call out. But it is not free from attack: A relatively small input can lead to memory exhaustion or to C stack exhaustion, crashing the process. There is also the possibility for excessive CPU consumption denial of service on some inputs. Calling it on untrusted data is thus not recommended.

import ast

data = {'expert_interests': "['Machine learning', 'deep learning']"}

data["expert_interests"] = [
  {"title": title}
  for title
  in ast.literal_eval(data["expert_interests"])
]

print(data)
# {'expert_interests': [{'title': 'Machine learning'}, {'title': 'deep learning'}]}

Another option is to use json.loads to convert it to a list of strings, then just make each a dictionary.

The only problems with this are that it blindly replaces ' with " (since the JSON standard only uses " for strings) which can lead to errors if you have " or ' anywhere in your string, but this is better than eval because eval is dangerous especially when you can’t trust the source.

import json

data = {'expert_interests': "['Machine learning', 'deep learning']"}

data["expert_interests"] = [
  {"title": title}
  for title
  in json.loads(data["expert_interests"].replace("'", '"'))
  # only use this line (instead of above) if you trust the source of this data
  # in eval(data["expert_interests"])
]

print(data)
# {'expert_interests': [{'title': 'Machine learning'}, {'title': 'deep learning'}]}
Answered By: Samathingamajig
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.