Simple problem

Gerhard Häring gh at ghaering.de
Thu Nov 13 11:50:21 CET 2003


Angelo Secchi wrote:
> Hi,
> I'm newby to Python with the following problem.
> In one of my programs I have the following loop:
> 
> for index in markets:
>     pippo='firm_'+index
>     
> 
> where markets is a list ['A','B','C']. In the same loop at each step I
> would like to create a new list with name 'firm_A', 'firm_B' and so on.
> How can I ask to python to do that?

You can do almost anything in Python , because it is so dynamic. A lot 
of what you *can* do isn't necessarily a good idea, though. Dynamically 
creating variable names like you requested for example is certainly *no* 
good idea.

Instead, just use a *dictionary* (please refer to the tutorial for what 
dictionaries are, and how you use them). You'll probably want a 
dictionary 'firms' with the market as a key, something like:

for market in markets:
     firm[market] = []
     # ...

-- Gerhard






More information about the Python-list mailing list