-1

I am looping through several lists. Then I want to loop through their elements but each list requires different loop statements (e.g. take only every 3nd value from list 1 vs. multiply each value by 2 in list 2.).

Would it make sense to create a py file with if-elif for each list name and return list-specific loop statements? How could this be implemented? Any other ideas?

e.g.

def di_xyz(x):
    if x == 'list1':
    x = x+2
    else:
    x = x-2

newlist = []
list1 = [1,2,3]
list2 = [4,5,6]

for x in list_of_lists:
    do_xyz(x)
    newlist.append(x)



output:
new_list=[3,4,5,2,3,4]
regresss
  • 15
  • 6

1 Answers1

0

your question in not clear enough but i will try to help you as much i a could understand from your post and your code.

listOfList = [
    [1,2,3],
    [4,5,6]];

newList = [];

for idx, l in enumerate(listOfList):
    if idx == 0:
        for x in l:
            newList.append(x+2)
    else:
        for x in l:
            newList.append(x-2)

print(newList)

by the way, i don't get it way you want to call another python function to run a piece of code. you have functions, use them to make things clear. and in this example you can avoid using the (if idx == 0 .... else ) and try to run the first list in the listOfList first and then use loop for the rest of the listOfList.

shadow
  • 767
  • 3
  • 8
  • 20
  • Thanks shadow but would this not be more difficult to maintain if you had many more lists? Also, you are essentially repeating for x in l for each list. The idea is to maintain the loop statements (x+2, x-2 etc.) in different script. – regresss Oct 19 '19 at 07:16
  • what i get from you, is, you want to do something (some kind of processing you called a script which is a piece of code) for each list in list of lists. for example you want to do something for list 1, other processing for list 2, other processing for list 3, etc ..., but to make it clear for both of us, the different scripts (code or processing) you want to run is determent which mean you know how many codes (scripts) you gonna run, so for each script (code) write it in a function and you can call it "functionForList1(), functionForList2, etc), " continue in next comment" – shadow Oct 19 '19 at 22:59
  • if the process is the same and only some kind of variable need to change for each list, then write one function and pass a variable (argument) to that function depends on the list index. now, if you want to run all lists together or you want some sort of efficiency, then use multi-threading and run each code for each list in separate thread. the reason why you need to avoid something like using another script is the process time and the complexity of code and process that would take you to accomplish something you could just do by using function and threading. – shadow Oct 19 '19 at 23:03
  • " The idea is to maintain the loop statements" if you mean by that, you want an infinite loop for each list, multi threading can do that for you. i don't know what you want to do by infinite loop, it's your project, but the solution can be, run (create and launch) a thread (task) for each list and pass the list as argument and that thread will run your piece of code as long as you want. – shadow Oct 19 '19 at 23:08
  • I want to pass variable(argument) depending on list index. My issue is how to pass loop statement(s) or even the whole loop from 2nd script to 1st script. – regresss Oct 20 '19 at 08:07
  • I'm kinda curious about this project you are doing. and for the question "passing loop statement(s) from 2nd script to 1st script" the only thing i can think about is passing a function as argument to another function, we use this in callbacks where you have to declare a function (let's called A) and pass it to another function (B) as an argument so the function B can use it for some sort of process. this is an example http://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/ , you can look for more in the web. – shadow Oct 20 '19 at 20:05
  • To be honest with you. i still don't understand your real problem, or the reason behind trying to use another file to run a piece of your code and get the results from it. i can give you a solution for that. but the reason of using that is not clear for me. (here some help: https://stackoverflow.com/questions/6086047/get-output-of-python-script-from-within-python-script or https://stackoverflow.com/questions/3781851/run-a-python-script-from-another-python-script-passing-in-arguments). and again, without knowing the question (your project) i'm not convince that you need this technique. – shadow Oct 20 '19 at 20:11