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
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)
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)
Write the output of the following Python program code:
Str2 = list("Cam@123*") for i in range(len(Str2)-1): if i==5: Str2[i] = Str2[i]*2 elif (Str2 [i].isupper()): Str2 [i] = Str2 [i]*2 elif (Str2 [i].isdigit()): Str2 [i] = 'D' print(Str2)
Differentiate between append() and insert() methods of list.