Search code examples
pythondictionaryassignmultiple-value

How to assign multiple values to multiple keys with a single command


I have dictionary like:

Info = {
  "City_Name" : {
    "Population" : None,
    "Population_Density" : None
    }
}

I want to assign values to "Population" and "Population_Density" keys. I actually can do that with the use of the following commands:

Info["City_Name"]["Population"] = 20000 
Info["City_Name"]["Population_Density"] = 200 

But instead, I want to do that with a single command like:

Info["City_Name"]["Population","Population_Density"] = 20000 , 200

But this doesn't work, the command above generates a new key... (In fact, a function returns me those values, and therefore, I need to do that with a single command)

Edit:

I needed to mention; without using:

Info["City_Name"]["Population"],Info["City_Name"]["Population_Density"] = 20000, 200

The key-names of my dictionary are so long that, it is hard to follow; they take a lot of space. I also need to assig three values to three keys. Therefore, I was wondering if there is any way to do that with just a single modification on the part, that is different than each other (eg; "Population" and "Population_Density").


Solution

  • Try this:

    Info["City_Name"].update({"Population": 20000, "Population_Density": 200})