0

this may sound like a strange question from a Python noob, but here's the deal. I have a list with a bunch of (string) entries. I want to take one of those entries, add another string to the end of it, and then create a new array with that name. Eg, I have

list=["foo","bar"]

and I want to get something to the effect of

fooblah = []

I've been trying to do it this way

list[0] + "blah" = []

Obviously this doesn't work because the first part is a string instead of a variable name, but I'm not sure how to fix it. From what I've read of other people's problems, the solution might be in using a dictionary instead (?), but to be honest I'm not really sure how dictionaries work yet.

Thanks, J.

1
  • You can think of a dictionary as a type that maps one thing to another -- just like a real dictionary maps words to their definitions. You still access elements through the [ ] operator just like arrays, except that the keys don't have to be integers. Commented Mar 11, 2011 at 5:25

2 Answers 2

6

I would use a python dictionary they can use practically anything as an index instead of integers. You're code could get messy and unreadable really fast if you are dynamically creating local / global variables.

For example

l = ["foo","bar"]
d = {}
d[l[0] + "blah"] = []
d[l[0] + "blah"].append("foo")

Using a dictionary will give you some other neat advantages, for example, you can loop through all of the new lists you create.

for k,v in d.iteritems():
    print "{0} --> {1}".format(k,v)
Sign up to request clarification or add additional context in comments.

Comments

1

Well, if you really want to do this one way would be through the globals() or locals() methods which return a dictionary of the current symbols:

globals()[list[0] + 'blah'] = [  ]

Note that list is the name of a built-in type, so using it as your variable name is considered bad practice.

Also, this is a highly unusual need; what exactly are you trying to accomplish? Perhaps there is a better way?

3 Comments

Yeah, sorry, it's not actually the variable name; I was just using it as an example (obviously not a very good one). Basically I have a list of all the directories in a folder, each one named for a different exposure. If the directory has a certain file in it, I want to read that whole file into a new array, but to keep each exposure separate it would be nice if that new array could somehow be named in a way that lets me know which exposure it came from. Is there a better way? I don't doubt it, but like I said I'm pretty new at this.
@JustinS: Definitely a case for dictionaries (see GWW's answer). The exposure folder name could be the key, with the file contents as the value (if I interpreted correctly). Then you could do all sorts of nice things with the dictionary that you couldn't with variables, such as loop over them easily.
Yup, that's what we're going to go with. Thanks for your and @GWW 's advice.

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.