Issue
I'm a begginer in python and I want to make this:
I have a string array I want to make a dictionary with string as keys, but this way: Transform this:
['Users', 'ID', 'Age']
Into this:
{
'Users': {
'ID': {
'Age': None
}
}
}
Solution
You could do this like so:
def tranform_list_to_dict(lst):
new = {}
for item in lst[::-1]:
if not new:
new[item] = None
else:
tmp = {}
tmp[item] = new.copy()
new = dict(tmp)
return new
my_list = ['Users', 'ID', 'Age']
print(tranform_list_to_dict(my_list))
Which will produce:
{
"Users": {
"ID": {
"Age": None
}
}
}
Answered By - Aleph-null
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.