CBSE Class 12 Computer Science
Question 51 of 68
Data Structures - I : Linear Lists — Question 7
Back to all questionsEnter a list : [12, 3, 4, 5, 7, 12, 8, 23, 12]
False
Enter a list : [8, 9, 2, 3, 7, 8]
True
s = eval(input("Enter a list : "))— This line prompts the user to enter a list.n = len(s)— This line finds the length of the lists.t = s[1:n-1]— This line slices the listsstarting from index 1 up to index n-1, which effectively removes the first and last elements from the listsand stores it in listt.print(s[0] == s[n-1] and t.count(s[0]) == 0)— The conditions[0] == s[n-1] and t.count(s[0]) == 0checks if the first and the last element of the listsare same [s[0] == s[n-1]] and that element does not appear anywhere else in listsapart from the first and last position [t.count(s[0]) == 0]. For case (i), 12 is the first and the last element of list but as it also occurs at the 5th index hence the output isFalse. For case (ii), 8 is the first and the last element of list and it doesn't appear anywhere else hence the output isTrue.