I am trying to understand if it makes sense to take the content of a list and append it to another list.
I have the first list created through a loop function, that will get specific lines out of a file and will save them in a list.
Then a second list is used to save these lines, and start a new cycle over another file.
My idea was to get the list once that the for cycle is done, dump it into the second list, then start a new cycle, dump the content of the first list again into the second but appending it, so the second list will be the sum of all the smaller list files created in my loop. The list has to be appended only if certain conditions met.
It looks like something similar to this:
# This is done for each log in my directory, i have a loop running for logs in mydir: for line in mylog: #...if the conditions are met list1.append(line) for item in list1: if "string" in item: #if somewhere in the list1 i have a match for a string list2.append(list1) # append every line in list1 to list2 del list1 [:] # delete the content of the list1 break else: del list1 [:] # delete the list content and start all over
Does this makes sense or should I go for a different route?
I need something efficient that would not take up too many cycles, since the list of logs is long and each text file is pretty big; so I thought that the lists would fit the purpose.
You probably want
Here's the difference:
>>> a = range(5) >>> b = range(3) >>> c = range(2) >>> b.append(a) >>> b [0, 1, 2, [0, 1, 2, 3, 4]] >>> c.extend(a) >>> c [0, 1, 0, 1, 2, 3, 4]
list.extend() accepts an arbitrary iterable, you can also replace
for line in mylog: list1.append(line)
Take a look at itertools.chain for a fast way to treat many small lists as a single big list (or at least as a single big iterable) without copying the smaller lists:
>>> import itertools >>> p = ['a', 'b', 'c'] >>> q = ['d', 'e', 'f'] >>> r = ['g', 'h', 'i'] >>> for x in itertools.chain(p, q, r): print x.upper()