I have list like this: listvalues = [True, True, False, True, False, 24, somestring, 50]
So the first five elements of the list are booleans, the rest are strings and ints.
Now I want to check the first five elements if they are true or false, and if they are true,
I want to use a certain method, depending on the index of the element.
So, in the case above, one action should be made for the first element (index 0), then another action for the second element (index 1), then another action for the forth element.
If all of the booleans are false a warning should appear.
Sorry for the noob question, but I couldn’t find a short and easy solution by myself…
I have list like this: listvalues = [True, True, False, True, False, 24, somestring,
Share
There are a number of things that might help here. First off, you should use list slicing to isolate the booleans. If you know the first 5 elements are what you’re after, then
myList[0:5]will get just those and ignore the others.Next, look into the
anyfunction. It’ll returnTrueif any of the elements is True, so you might do this to decide if you need to issue the warning:Next, to deal with which functions to call, you’d probably want something like this:
Ideally though, what would probably work better would be to put references to the individual functions in the original list, instead of booleans, with a value of None if you wanted to skip that function. Your original list would end up looking like this:
To call the ones that you want to call you’d do this: