KnowledgeBoat Logo
|
LoginJOIN NOW

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[::2]
print(myList)

Python List Manipulation

1 Like

Answer

[2, 4, 6, 8, 10]

Working

In the given code, myList is initialized as [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. The del myList[::2] statement deletes elements in the list with a step of 2, starting from the beginning of the list. This means, it removes every second element from myList. Specifically, it deletes elements at indices 0, 2, 4, 6, and 8, which correspond to the values 1, 3, 5, 7, and 9. The subsequent print(myList) statement then outputs the modified list, which is [2, 4, 6, 8, 10].

Answered By

2 Likes


Related Questions