Given the following assignment of a variable LS = [1, 9, 2, 8, 3, 7, 5, 4]
Given the following assignment of a variable LS
= [1, 9, 2, 8, 3, 7, 5, 4]
What will be the output of the following?
print (LS [::2]+ LS [::-2])
Show answerHide answer
Correct answer: (B) [1, 2, 3, 5, 4, 7, 8, 9]
Explanation
LS[::2] takes indexes 0, 2, 4, 6, giving [1, 2, 3, 5]. LS[::-2] starts from the last element and goes backwards in steps of 2, giving [4, 7, 8, 9]. Joined, they give [1, 2, 3, 5, 4, 7, 8, 9].