0

I've got a problem of appending elements in multiple lists. My program goes like this. where list is just a series of numbers.

for j in range(0, len(list)):
    if (int(list[j][4]) == 0 or int(list[j][:-4]) == 41601000000):
        filelist0.append(list[j])
    if (int(list[j][4]) == 1 or int(list[j][:-4]) == 41602000000):
        filelist1.append(list[j])
    if (int(list[j][4]) == 2 or int(list[j][:-4]) == 41603000000):
        filelist2.append(list[j])
    if (int(list[j][4]) == 3 or int(list[j][:-4]) == 41604000000):
        filelist3.append(list[j])
    if (int(list[j][4]) == 4 or int(list[j][:-4]) == 41605000000):
        filelist4.append(list[j])
    if (int(list[j][4]) == 5 or int(list[j][:-4]) == 41606000000):
        filelist5.append(list[j])
    if (int(list[j][4]) == 6 or int(list[j][:-4]) == 41607000000):
        filelist6.append(list[j])
    if (int(list[j][4]) == 7 or int(list[j][:-4]) == 41608000000):
        filelist7.append(list[j])
    if (int(list[j][4]) == 8 or int(list[j][:-4]) == 41609000000):
        filelist8.append(list[j])

This is ugly. Is there a way to write the above code in a line or two? Obvious I can iterate strings, but not for the name of the lists. (can't iterate filelist[m] for m in range(0, 9)).

Thanks!

1
  • Tip: Don't use list as a variable name. Subtle mayhem ensues. Commented May 1, 2013 at 21:46

5 Answers 5

3

The following should be equivalent:

filelists = [filelist0, filelist1, filelist2, filelist3, filelist4,
             filelist5, filelist6, filelist7, filelist8]
for x in lst:
    for i, filelist in enumerate(filelists):
        if int(x[4]) == i or int(x[:-4]) == 41601000000 + i * 1000000:
            filelist.append(x)

Note that I renamed list to lst, it isn't a good idea to used built-in names for your variables.

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

2 Comments

+1, but there's no need for the j variable. A loop over x in lst would be even shorter.
Shouldn't that be if int(x[4]) == i or ...
1

If you are using Python 2.7 you can actually use the map() function without a variable assigned, e.g.

>>> listdata = [0, 1, 2, 3, 4]
>>> list_o_lists = [ls0, ls1, ls2, ls3, ls4]
>>> map(lambda x, y: x.append(y), listdata, list_o_lists)

Hope this helps, thought it was a bit simpler than the other answers.

Comments

0

Why not put the lists in a dict? You can use defaultdict to automatically create the item if it's not already in there. The increment number on the end is done by %d into a string then using int to convert back to a number. Still ugly, but there you go.

from collections import defaultdict

filelist = defaultdict(list)

for j in range(0, len(list)):
    for fl in range(0, 9) #0-8
        if (int(list[j][4]) == fl or int(list[j][:-4]) == int('4160%d000000' % fl+1)):
            filelist[fl].append(list[j])

Comments

0

This solution with one list of lists

filelists = []

list = [[1,3,2,6,5,7,6],[3,5,7,8,5,5,4]]

for i in range(8):
    filelists.append([])
    for j in range(0, len(list)):
        if (int(list[j][4]) == i or int(list[j][-4]) == 41601000000):
            print 'added'
            filelists[i-1].append(list[j])

Result is

>>> filelists
[[], [], [], [], [[1, 3, 2, 6, 5, 7, 6], [3, 5, 7, 8, 5, 5, 4]], [], [], []]

Comments

-2
filelist = [[] for _ in range(9)]
for j, item in enumerate(item_list):
    for i in range(9):
        if (int(item[4])==i or
            int(item[-4])==int('4160{}000000'/format(i+1))
        ):
            filelist[i].append(j)
            break

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.