417 B
417 B
Lessons Learned from LeetCode
Slicing
- How to use slicing for shifting elements
my_list = [1, 2, 3, 4, 5]
# Shift list to the right using positive slicing
shifted_positive = my_list[-2:] + my_list[:-2]
print(shifted_positive) # Output: [4, 5, 1, 2, 3]
# Shift list to the left using negative slicing
shifted_negative = my_list[2:] + my_list[:2]
print(shifted_negative) # Output: [3, 4, 5, 1, 2]