English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I need to make the value of

"acc" + str(len(accounts)+1)

the name of a new list.... anyone any ideas?

2007-03-20 11:09:46 · 1 answers · asked by uktshaw 3 in Computers & Internet Programming & Design

1 answers

You could use the locals() dictionary, which gives access to all the defined local variables:
varname = "acc" + str(len(accounts)+1)
locals()[varname] = mylist

However this may not be the best way of approaching the problem. Have you thought of using a dictionary for the collection of lists?

listdic = {'acc1': list1, 'acc2': list2}
listdic[varname] = list3

Also note that there's an easier way of creating the variable name, using string substitution:
"acc%1" % (len(accounts)+1)

This even works as a dictionary key:
listdic["acc%1" % (len(accounts)+1)]

2007-03-21 02:22:37 · answer #1 · answered by Daniel R 6 · 0 0

fedest.com, questions and answers