[Tutor] extending a series of lists

Magnus Lyckå magnus@thinkware.se
Wed Jul 2 15:49:02 2003


At 17:33 2003-07-02 +0000, tutor.python.org wrote:
>I have a series of lists called "line1", "line2", "line3", etc... which I 
>combine into one list this way:
>
>c4 = []
>for i in range(46):
>exec('c4.extend(line' + str(i+1) + ')')

Ouch! Don't use "list1", "list2" etc. Use a list of lists.
I'd say that as soon as you write variable names with numbers
like that, you are just trying to implement a list very poorly. ;)

I.e. instead of having:

list1 = [...]
list2 = [...]
...

you should have:

lists = []
lists.append([...])
lists.append([...])
...

so in a place where you would use "list0" today, you use
"lists[0]" instead. Where you want to iterate over the
lists, simply use something like:

c4 = []
for l in lists:
    c4.extend(l)


--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language