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 ?

4 comments

@stud3nt 2020-02-14 11:38:02

You can use a single line of python code in for to achieve your result.

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

Output

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

@Gaurav Shimpi 2020-02-14 08:34:16

This might useful.

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

Output:

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

@Shubham Shaswat 2020-02-14 08:21:17

Try this way:

dic2 ={}
file_list = ["file1","file2", "file3"]
for f in file_list:
 dic1 ={}

 dic1["filename"] = f
 dic1["namelen"] = len(f)
 dic2[f] = dic1
print(dic2)

each and every iteration try creating a new dict1={} inside the loop

Otherwise,if you do outside the loop,it will reference the same dictionary again and again

@Austin 2020-02-14 08:21:05

Add a copy of dictionary instead of the dictionary itself.

This line:

dic2[file] = dic1

changes to:

dic2[file] = dic1.copy()

@3lokh 2020-02-14 08:28:06

yes this works ! thanks. So when doing dic2[file] = dic1 don't copy the data but only the reference of the object. What ever is the last value of dic1 will be there in dic2 for all embedded dictonaries !

@Austin 2020-02-14 08:34:33

Yeah, dic2[file] = dic1 is adding reference (like, pointer). When the value pointing to that changes, all references are updated.

Related Questions

Sponsored Content

16 Answered Questions

[SOLVED] How can I add new keys to a dictionary?

63 Answered Questions

[SOLVED] Calling an external command from Python

23 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

29 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3664407 View
  • 3011 Score
  • 29 Answer
  • Tags:   python list indexing

16 Answered Questions

[SOLVED] Check if a given key already exists in a dictionary

  • 2009-10-21 19:05:09
  • Mohan Gulati
  • 3358461 View
  • 2683 Score
  • 16 Answer
  • Tags:   python dictionary

18 Answered Questions

[SOLVED] What are metaclasses in Python?

18 Answered Questions

[SOLVED] Are static class variables possible in Python?

11 Answered Questions

[SOLVED] How to remove a key from a Python dictionary?

10 Answered Questions

[SOLVED] Static methods in Python?

Sponsored Content