Python List Slicing Questions
Python List Slicing Questions
my_list = [1, 2, 3, 4, 5]
print(my_list[1:4])
2. Write the slicing expression to extract the first three elements from the list my_list = [10, 20, 30,
40, 50].
print(my_list[:3])
4. Write the slicing expression to get all elements of the list except the last one.
print(my_list[2:])
6. Write the slicing expression to reverse the list my_list = [1, 2, 3, 4, 5].
print(my_list[-3:-1])
my_list = [1, 2, 3]
print(my_list[1:10])
9. Extract every second element from the list my_list = [1, 2, 3, 4, 5, 6, 7, 8].
10. Write a slicing expression to extract the last three elements of my_list = [5, 10, 15, 20, 25, 30].
print(my_list[::2])
12. Extract all elements of the list except the first two using slicing.
my_list = [1, 2, 3, 4, 5]
print(my_list[-1:-5:-1])
14. Write a slicing expression to extract every third element from the list starting at index 1.
print(my_list[-2::-1])
16. How can you extract the middle three elements from a list my_list = [10, 20, 30, 40, 50, 60, 70]?
17. Write a slicing expression to extract a sublist from index 2 to 8 with a step of 2.
my_list = [1, 2, 3, 4, 5, 6]
sub_list = my_list[::-2]
19. Write a slicing expression to create a copy of the entire list my_list.
print(my_list[1:4:2])