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