0

I want to run a list of functions, where each function should take one item from a list as parameter.

Func1 parameter takes first list value, Func2 parameter takes second list value...and so on.

So far, I have the following:

main.py

#import the 5 python files
import sheet_1 as s1
import sheet_2 as s2
import sheet_3 as s3
import sheet_4 as s4
import sheet_5 as s5

#save the functions in a list
func_lst = [s1.func1, s2.func2, s3.func3, s4.func4, s5.func5] 

#save workbook key as list
sheet_lst = ["Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5"]

sheet_1.py

def sheet1(n): #n param should take "Sheet1"
    print(n*2) #prints "Sheet1Sheet1"

sheet_2.py

def sheet2(n): #n param should take "Sheet2"
    print(n*2) #prints "Sheet2Sheet2"

sheet_3.py

def sheet3(n): #n param should take "Sheet3"
    print(n*2) #prints "Sheet3Sheet3"

And so on...

To make it more visual, this is how the code should look/work like:

#for first iteration
for item in sheet_lst:  #take "Sheet1"
for f in func_lst: #take s1.func1
        f(item) #run function with item parameter.

The end result of the loop would be:

s1.func1("Sheet1"); 
s2.func2("Sheet2"); 
s3.func3("Sheet3"); 
s4.func4("Sheet4"); 
s5.func5("Sheet5");

Does anyone have any clue on how to structure the loop?

Thanks in advance!

3
  • Not sure of goal. Given list of functions func_lst and list of sheets wbkey, are you trying to apply first function func_list[0] to wbkey[0], func_list[1] to wbkey[1],...etc.? Commented Apr 15, 2020 at 15:46
  • Sorry about the confusion. I want to send item from list, as parameter for each function. >>s1.func1("Sheet1"); >>s2.func2("Sheet2"); >>s3.func3("Sheet3"); >>s4.func4("Sheet4"); >>s5.func5("Sheet5"); Commented Apr 15, 2020 at 16:05
  • @DarrylG I will edit the question to make it more clear. Commented Apr 15, 2020 at 16:08

1 Answer 1

1

Use zip to produce a loop like you describe

for wbkey, f in zip(sheet_lst, func_lst):
   f(wbkey)

The result of the above is:

s1.func1("Sheet1"); 
s2.func2("Sheet2"); 
s3.func3("Sheet3"); 
s4.func4("Sheet4"); 
s5.func5("Sheet5");
Sign up to request clarification or add additional context in comments.

5 Comments

@DarryIG For some reason, it goes only until s4.func4. Leaving s5.func5 out.
@Raccoon_17--zip will iterate to the length of the shorter length. Are you certain you placed 5 elements in sheet_lst and func_list?
@DarryIG I added some more items but both lists have the same number of items. func_lst has 10 functions and sheet_lst has also 10 items in the list.
@Raccoon_17--what do you get with: print(list(zip(sheet_lst, func_lst)). It should be a list of tuples with the first element of each tuple from sheet_list and the second from func_list.
@DarryIG It finally worked! You were right! Thank you a lot for your help!! :)

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.