Map and Slicing
Map and Slicing
1. Reverse a List:
a. Write a program that takes a list of numbers as input and prints the list in
reverse order using slicing.
Example:
numbers = [1, 2, 3, 4, 5]
# Output should be [5, 4, 3, 2, 1]
Example:
3. Extract a Sublist:
a. Given a list of numbers, extract and print a sublist from index 2 to index 4
(inclusive) using slicing.
Example:
Example:
numbers = [1, 2, 3, 4, 5, 6]
# Output should be [2, 3, 4, 5]
5. Create a New List with Every Other Element:
a. Write a program that takes a list of numbers as input and creates a new
list that contains every other element from the original list using slicing.
Example:
Example:
numbers = [1, 2, 3, 4, 5]
# Output should be [1, 4, 9, 16, 25]
Example:
Example:
numbers = [1, 2, 3, 4, 5]
# Output should be [11, 12, 13, 14, 15]
Example:
Example:
Example:
numbers = [1, 2, 3, 4, 5]
# Output should be [25, 16, 9, 4, 1]
Example:
numbers = [1, 2, 3, 4, 5, 6]
# Output should be [2, 6, 10]
3. Apply a Function to Each Slice:
a. Given a list of numbers, extract every other element (use slicing) and then
apply a function that subtracts 5 from each number using map().
Example:
Example:
word = "abcdefghijk"
# Output should be [3, 3, 3, 3, 3]
Example:
numbers = [1, 2, 3, 4, 5, 6]
# Output should be [6, 7, 8] for the first half and [9, 10, 11] for
the second half
Example:
list1 = [1, 2, 3, 4, 5, 6]
list2 = [10, 20, 30, 40, 50, 60]
# Output should be [11, 33, 55] (after applying map to sliced lists)