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

1
  • 1
    Try Info["City_Name"]["Population"],Info["City_Name"]["Population_Density"]=20000,200 Commented Jan 29, 2014 at 20:30

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

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".
I tried different combinations of parentheses, curly braces etc. but, it didn't help...
Not without storing an intermediate variable for Info["City_Name"] and then using that on a subsequent line
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?
1

Try this:

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

5 Comments

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!)
So does your function return a list of values e.g. [20000, 200]?
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.
What about Info["City_Name"].update(dict(zip(["Population", "Population_Density"], myfunction())))?
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.