Informatics Practices
What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del myList[3:]
print(myList)
Answer
[1, 2, 3]
Working
In the given code, myList is initialized as [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. The del myList[3:] statement deletes all elements in the list starting from index 3 to the end. This modifies myList to only contain the elements from index 0 to 2. The subsequent print(myList) statement then outputs the modified list [1, 2, 3].
Related Questions
What will be the output of the following statement?
list1 = [1, 2, 3, 4, 5] list1[len(list1)-1]What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for i in range(0, len(myList)): if i % 2 == 0: print (myList[i])What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] del myList[:5] print(myList)What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] del myList[::2] print(myList)