newWithPython
newWithPython

Reputation: 883

Problems with a python function that return a list of strings?

I have a set of files in a directory. So I created a function that apply some processing to all the files in the directory:

def fancy_function(directory, regex):
    for set the path of the directory:
       with open the file names and walk over them:
           preprocessing1 = [....]
           preprocessing2 = [ remove some punctuation from preprocessing1]

           return preprocessing2

Then I do the following:

list_of_lists = fancy_function(a_directory, a_regex)
print list_of_lists
>>>['processed string']

It just return one list and the directory actually have 5 files, then when I do the following:

def fancy_function(directory, regex):
    do preprocessing...
    preprocessing1 = [....]
    preprocessing2 = [ remove some punctuation from preprocessing1]
    print preprocessing2

print fancy_function(a_directory, a_regex)

It returns the 5 preprocessed files that I want like this:

['file one']
['file two']
['file three']
['file four']
['file five']

Why is this happening and how can I obtain the 5 files in a list?. I would like to save them In one list in order to make a nother processing but now for each list in the main list, something like this:

main_list =[['file one'], ['file two'], ['file three'], ['file four'], ['file five']]

Upvotes: 1

Views: 2260

Answers (1)

zehnpaard
zehnpaard

Reputation: 6223

You have a return statement inside a for loop, which is a common gotcha. The function ends immediately, returning a single element, instead of returning a list of all the processed elements.

You have two options. First, you can explicitly define a list within your function, append intermediate results to that list, and return the list at the end.

def fancy_function(directory, regex):
    preprocessed_list = []
    for set the path of the directory:
        with open the file names and walk over them:
            preprocessing1 = [....]
            preprocessing2 = [ remove some punctuation from preprocessing1]

            preprocessed_list.append(preprocessing2)
    return preprocessed_list

Or fancier, you can turn your function into a generator.

def fancy_function(directory, regex):
    preprocessed_list = []
    for set the path of the directory:
        with open the file names and walk over them:
            preprocessing1 = [....]
            preprocessing2 = [ remove some punctuation from preprocessing1]

            yield preprocessing2 # notice yield, not return

This generator can then be used thus:

>>> preprocessed = fancy_function(a_directory, a_regex)
>>> print list(preprocessed)
[['file one'], ['file two'], ['file three'], ['file four'], ['file five']]

Upvotes: 3

Related Questions