2

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").

T-800
  • 1,543
  • 2
  • 17
  • 26

2 Answers2

2

Only way to do exactly what you're asking is:

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

Otherwise it takes Info["City_Name"], and creates a new key ("Population", "Population_Density") (a tuple), and assigns another tuple, (20000, 200) to that

Or you can do it in two lines:

d = Info["City_Name"]
d["Population"], d["Population_Density"] = 20000, 200
mhlester
  • 22,781
  • 10
  • 52
  • 75
  • That's true, thanks a lot. But, the key-names of my dictionary are so long that, it is hard to follow; it takes a lot of space. (I am actually assigning three values to three keys). Therefore, I was wondering if there is any way to just to modify the part, which is different than each other. In our case; "Population" and "Population_Density". – T-800 Jan 29 '14 at 20:36
  • I tried different combinations of parentheses, curly braces etc. but, it didn't help... – T-800 Jan 29 '14 at 20:38
  • Not without storing an intermediate variable for `Info["City_Name"]` and then using that on a subsequent line – mhlester Jan 29 '14 at 20:39
  • If I will not receive any solution, I can do your suggestion. I tried to implement it but I couldn't. How can I do that; how can we store an intermediate variable for such a string? – T-800 Jan 29 '14 at 21:13
1

Try this:

Info["City_Name"].update({"Population": 20000, "Population_Density": 200})
dusan
  • 9,104
  • 3
  • 35
  • 55
  • I get the numerical values from a function, and therefore, I cannot assign the values to key-names like the way you proposed. (For such a use; , I have to assign the values that my function returns to an intermediate variable. And then, I have to assign them the way you proposed. However, it is not the way I want to do it... But, anyway, thanks a lot for your response!) – T-800 Jan 29 '14 at 22:08
  • So does your function return a list of values e.g. `[20000, 200]`? – dusan Jan 30 '14 at 12:06
  • No, it actually returns arrays; my function is like: `def myfunction(): ... return array1, array2, array3` So, I have an array instead of 20000 and another array of values instead of 200... But leaving the array structure aside, I even do not know how to implement it even if my function would have returned numerical values. – T-800 Jan 30 '14 at 16:21
  • 1
    What about `Info["City_Name"].update(dict(zip(["Population", "Population_Density"], myfunction())))`? – dusan Jan 30 '14 at 16:48
  • That's it! I have combined your solution with @mhlester 's solution; so; `d= Info["City_Name"]` and then: `d.update(dict(zip(["Population", "Population_Density"], myfunction())))` . Now, it is exactly as I like to have. Thanks a lot! – T-800 Jan 30 '14 at 17:46