Adding dictonary with static keys inside another dictonary in python 3

Related searches

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 ?

Add a copy of dictionary instead of the dictionary itself.

This line:

dic2[file] = dic1

changes to:

dic2[file] = dic1.copy()

Python Dictionary, Check out what is a dictionary in Python, how to create, append, update, and Please remember that only keys inside a dictionary can't have duplicates, but While defining static dictionary objects, you must be careful to use unique values for keys. On adding a fresh “key: value” pair, a new element will get added to the� Do you want to insert one dictionary into the other, as one of its elements, or do you want to reference the values of one dictionary from the keys of another? Previous answers have already covered the first case, where you are creating a dictionary within another dictionary.

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

How can I add new keys to a dictionary?, data = {'a': 1, 'b': 2, 'c': 3} # OR data = dict(a=1, b=2, c=3) # OR data = {k: v for k, "Is it possible to add a key to a Python dictionary after it has been created? our dict has already been created and came from or was used for another purpose: It accepts in iterable sequence of key value pairs that can be single key value pair or list of tuples or an another dictionary. For each entry in sequence, it will add the given key value pair in dictionary and if key already exists then it will update its value.

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}
}

Dictionary Data Structures in Python 3, Dictionaries map keys to values, making key-value pairs that can then store data. In this tutorial, we will go over the dictionary data structure in Python. In this case, we'll reference an existing key and pass a different value to it. Just as you can add key-value pairs and change values within the dictionary data type, you� Python | Add new keys to a dictionary Last Updated: 21-11-2018 Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair.

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}
}

Python Dictionaries, In this tutorial you will learn about Dictionaries in python. Also, unlike an index, keys can be of any data type varying from integer to string. Key-3, Element-3 We can easily add elements to an empty dictionary after its initialization. For example, let's assume that there is another dictionary containing the list of the� Hi I have the below dictionary which has a value with a list, and inside the list is a dictionary. Is there a way to call the dictionary value inside the list using the key instead of the list index? The dictionary inside the list may vary so the index value may not always provide the right key value pair.

Python, Keys of a Dictionary must be unique and of immutable data type such as Strings, Integers and Nested Dictionary: Nesting Dictionary means putting a dictionary inside another dictionary. print ( "\nNested dictionary 3-" ) One way to add a dictionary in the Nested dictionary is to add values one be one,� get_name is a function that given a dictionary, returns the value of its "name" key. I.e., get_name = lambda x: x['name']. sorted returns the list of dictionaries sorted by the value of the "name" key. groupby returns an iterator of (name, dicts) where dicts is a list (ok, generator) of the dicts that share name as the value of the "name" key

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key : value pair.

Comments
  • Move dic1 ={} inside your for loop
  • dic2 is not valid Python, as it is it represents a set with dictionaries (which are not hashable)
  • @DaniMesejo, I have corrected in the first description. Sorry for that !
  • 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 !
  • Yeah, dic2[file] = dic1 is adding reference (like, pointer). When the value pointing to that changes, all references are updated.