Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- def list_manipulator(any_list, *args):
- command = args[0]
- position = args[1]
- nums = [int(n) for n in args[2:]]
- any_list = deque(any_list)
- if command == 'add' and position == 'beginning':
- any_list.extendleft(reversed(nums))
- elif command == 'add' and position == 'end':
- any_list.extend(nums)
- elif command == 'remove' and position == 'beginning':
- if nums:
- for _ in range(*nums):
- any_list.popleft()
- else:
- any_list.popleft()
- elif command == 'remove' and position == 'end':
- if nums:
- for _ in range(*nums):
- any_list.pop()
- else:
- any_list.pop()
- return list(any_list)
- print(list_manipulator([1,2,3], "remove", "end"))
- print(list_manipulator([1,2,3], "remove", "beginning"))
- print(list_manipulator([1,2,3], "add", "beginning", 20))
- print(list_manipulator([1,2,3], "add", "end", 30))
- print(list_manipulator([1,2,3], "remove", "end", 2))
- print(list_manipulator([1,2,3], "remove", "beginning", 2))
- print(list_manipulator([1,2,3], "add", "beginning", 20, 30, 40))
- print(list_manipulator([1,2,3], "add", "end", 30, 40, 50))
Advertisement
Add Comment
Please, Sign In to add comment