Informatics Practices
What will be the output of the following statement?
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1[::-2]
list1[:3] + list1[3:]
Python List Manipulation
4 Likes
Answer
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Working
In the given code, list1 is initialized as [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. The expression list1[::-2] creates a new list with elements from list1 in reverse order, taking every second element, resulting in [10, 8, 6, 4, 2], but this new list is not stored or used further. The expression list1[:3] + list1[3:] concatenates the first three elements [1, 2, 3] with the rest of the elements [4, 5, 6, 7, 8, 9, 10].
Answered By
3 Likes
Related Questions
What will be the output of the following statement?
list1 = [12, 32, 65, 26, 80, 10] list1.sort() print(list1)What will be the output of the following statement?
list1 = [12, 32, 65, 26, 80, 10] sorted(list1) print(list1)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])