There are too many questions of list comprehensions on this website, but none seem to match what I'd like to do. I have already done it with a for loop, but I was wondering if it could be done with list comprehensions, or if someone could direct me where I could find a similar case.
I have a list of functions:
function_list=[iron,cobalt,nickel,copper,zinc,vanadium,chromium,managenese,titanium,footer]
Each function is a question I ask to my students, exported to a PDF file. The last function on the list, is footer which makes the PDF file insert a page skip to the next page.
So usually, What does a simple quiz look like (so far)?
call_functions([x for x in function_list[0:3]] + [function_list[-1]])
generating
call_functions([iron,cobalt,nickel,footer]) #as desired
where call_functions is basically a PDF exporter. So my list comprehension adds three questions and skips to the next page, adding three more. As the number of questions grow, the code ends up looking like a mess:
call_functions([x for x in function_list[0:3]] + [function_list[-1]] + [x for x in function_list[3:6]]+ [function_list[-1]] + [x for x in function_list[6:9]])
generating
call_functions([iron,cobalt,nickel,footer,copper,zinc,vanadium,footer,chromium,managenese,titanium]) #as desired
although this works, I'm trying to make a single comprehension list that will iterate through the list and after every third element, it'll insert the last element on the list. Or even keep footer outside the list is also viable. But I can't get it to work.
I've tried:
[x for i,x in enumerate(function_list[0:9]) if i%3==0 function_list[-1] else x]
to SyntaxError.
Also tried:
[x if i%3==0 function_list[-1] else x for i,x in enumerate(function_list[0:9])]
Also to SyntaxError
Could someone tell me (or direct me to) what am I doing wrong, and/or direct to a similar case, please?
Thanks