What is the output of the following snippet?
my_list_1 = [1, 2, 3] my_list_2 = [] for v in my_list_1: my_list_2.insert(0, v) print(my_list_2)
- [3, 2, 1]
- [1, 2, 3]
- [3, 3, 3]
- [1, 1, 1]
Explanation: Let’s analyze this code:
- a list named my_list_1 is created with the elements 1,2,3,
- an empty list named my_list_2 is created,
- using a for loop, my_list_1 is iterated,
- every element in my_list_1 is inserted in my_list_2, always in the position 0,
- this is the list in each iteration:
- [1]
- [2,1]
- [3,2,1]
- finally, the list is printed on the console.
More Questions: Python Essentials 1 – Module 3 Test
Please login or Register to submit your answer