1

How can I assign a string value as an empty dictionary in Python? For example,

print hdr  
AB_1_15_Oct_2015  
hdr = {}  
hdr  
{}  

But I want AB_1_15_Oct_2015 to be an empty dictionary.

3
  • What do you mean I want to assign a string value as an empty dictionary? Are you talking about defining a variable using a string? Commented Oct 16, 2015 at 16:07
  • AB_1_15_Oct_2015 = {}?? Commented Oct 16, 2015 at 16:08
  • sam2090, Yes, That's correct. But I don't want to use AB_1_15_Oct_2015 to define an empty dict. Commented Oct 16, 2015 at 16:14

3 Answers 3

1

Took a while to figure out, but try this:

hdr = "AB_1_15_Oct_2015"
print hdr  
locals()[hdr] = {}

print AB_1_15_Oct_2015

Output:

AB_1_15_Oct_2015

{}

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

It's often considered a bad practice to dynamically assign variables like that. There are ways to do so, for instance using exec, but it is dangerous. A better idea would be to use a dictionary, since dynamically creating keys and values is simple. Slightly modifying your existing code:

hdr = "AB_1_15_Oct_2015"
someDict = {}
someDict[hdr] = {}

Then you could access it either from the top level

someDict[hdr]['a'] = "spam"

Or by assigning another variable to reference it:

hdrDict = someDict[hdr]
hdrDict['b'] = "parrot"

2 Comments

This solution too works for me. Thanks. I was struggling with the other way. Why do you call it dangerous?
The 'exec' and 'eval' statements allow Python to run strings as code. If these strings are coming from a user (as either text input, from a file, scanning file names a user may have access to, etc...) then it gives users a means to inject arbitrary code into your program. This is a huge security flaw. A less worse case scenario is that you are the one generating all the strings, and even then, an error can cause serious side effects, or problems that are difficult to debug.
0

If you are saying you want a dictionary named "AB_1_15_Oct_2015". You need to explicitly define a dictionary named:

AB_1_15_Oct_2015 = {}

If you mean you want to be able to set a variable name based off of a value in a string, then there is no way to accomplish this. Also, I can't think of a reason that you would want to. Variable names are only used internally in the code and therefore being able to change variable names based on the date wouldn't help you track a specific date. Without more information on what you are trying to accomplish, it's hard to be able to tell what you need, but I think something like this would accomplish what you want:

hdr = {
    "identifier": "AB_1_15_Oct_2015",
    "data": whatever_you_wanted_to_store
    }

that way that identifier is attached to the data in an accessible way.

Comments

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.