By 3lokh


2020-02-14 08:16:52 8 Comments

I wanna make a dictonary with another dictonary that have fixed keys e.g

dic1 = {'filename':'file1','namelen':'5'}
dic2 = {
"file1":dic1,
"file2":dic2,
...
...
}

The issue with my code is adding a new dictonary overwrites all the exisitng inner dictonary.

dic1 ={}
dic2 ={}
file_list = ["file1","file2", "file3"]
for file in file_list:
 dic1["filename"] = file
 dic1["namelen"] = len(file)
 dic2[file] = dic1
print(dic2)

My dictonary looks like this

    dic2 = 
{
'file1': {'namelen': 5, 'filename': 'file3'}, 
'file3': {'namelen': 5, 'filename': 'file3'}, 
'file2': {'namelen': 5, 'filename': 'file3'}
} 

How to prevent the overwrite, without switching to list ?

Related Questions

Sponsored Content

Sponsored Content