KnowledgeBoat Logo
|

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)

Python List Manipulation

4 Likes

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].

Answered By

1 Like


Related Questions