Python: Get a list with n elements removed from the left, right
Python List: Exercise - 268 with Solution
Write a Python program to get a list with n elements removed from the left and right.
Removed from the left:
- Use slice notation to remove the specified number of elements from the left.
- Omit the last argument, n, to use a default value of 1.
Removed from the right:
Returns a list with n elements removed from the right.
- Use slice notation to remove the specified number of elements from the right.
- Omit the last argument, n, to use a default value of 1.
Sample Solution:
Python Code:
def drop_left_right(a, n = 1):
return a[n:], a[:-n]
nums = [1, 2, 3]
print("Original list elements:")
print(nums)
result = drop_left_right(nums)
print("Remove 1 element from left of the said list:")
print(result[0])
print("Remove 1 element from right of the said list:")
print(result[1])
nums = [1, 2, 3, 4]
print("\nOriginal list elements:")
print(nums)
result = drop_left_right(nums,2)
print("Remove 2 elements from left of the said list:")
print(result[0])
print("Remove 2 elements from right of the said list:")
print(result[1])
nums = [1, 2, 3, 4, 5, 6]
print("\nOriginal list elements:")
print(nums)
result = drop_left_right(nums)
print("Remove 7 elements from left of the said list:")
print(result[0])
print("Remove 7 elements from right of the said list:")
print(result[1])
Sample Output:
Original list elements: [1, 2, 3] Remove 1 element from left of the said list: [2, 3] Remove 1 element from right of the said list: [1, 2] Original list elements: [1, 2, 3, 4] Remove 2 elements from left of the said list: [3, 4] Remove 2 elements from right of the said list: [1, 2] Original list elements: [1, 2, 3, 4, 5, 6] Remove 7 elements from left of the said list: [2, 3, 4, 5, 6] Remove 7 elements from right of the said list: [1, 2, 3, 4, 5]
Flowchart:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to get the cumulative sum of the elements of a given list.
Next: Write a Python program to get the every nth element in a given list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Returns True if the provided function returns True for every element in the list, False otherwise:
Example:
def tips_every(lst, fn=lambda x: x): return all(map(fn, lst)) print(tips_every([2, 4, 3], lambda x: x > 1)) print(tips_every([1, 2, 3]))
Output:
True True
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises