Question: #THIS IS THE CODE: tv = [ 'r24_getrenderingmode', 'r24_panel', 'r24_pcpar', 'Literal question:', 'Which party would you vote for today?', 'r24_pcpar_13_other', 'Literal question:', 'Other:', 'Post question:',
#THIS IS THE CODE: tv = [ 'r24_getrenderingmode', 'r24_panel', 'r24_pcpar', 'Literal question:', 'Which party would you vote for today?', 'r24_pcpar_13_other', 'Literal question:', 'Other:', 'Post question:', 'KIWI:', 'r24_pcpak', 'r24_pcpak_16_other', 'r24_pcpaf', 'Literal question:', 'Which party would you vote for?', ] rex = [] for i in tv: if i[0] == "r": rex.append(i) for i in range(len(tv)-1): if tv[i+1] in rex: print(tv[i]) if tv[i+1] not in rex: print("", tv[i]) The output is:
r24_getrenderingmode r24_panel r24_pcpar Literal question: Which party would you vote for today? r24_pcpar_13_other Literal question: Other: Post question: KIWI: r24_pcpak r24_pcpak_16_other r24_pcpaf Literal question:
How can I modify it so that the last elem not in rex is also included? DESIRED OUTPUT:
r24_getrenderingmode r24_panel r24_pcpar Literal question: Which party would you vote for today? r24_pcpar_13_other Literal question: Other: Post question: KIWI: r24_pcpak r24_pcpak_16_other r24_pcpaf Literal question: Which party would you vote for?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
