viligen

list_manipulator_2

Feb 16th, 2022
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. def list_manipulator(list_, com1, com2, *args):
  2.     result = []
  3.     if com1 == 'remove' and com2 == "end":
  4.         num = args[0] if args else 1
  5.         result = list_[:-num]
  6.     elif com1 == 'remove':
  7.         num = args[0] if args else 1
  8.         result = list_[num:]
  9.     elif com1 == 'add' and com2 == 'end':
  10.         result = list_ + list(args)
  11.     elif com1 == 'add':
  12.         result = list(args) + list_
  13.     return result
  14.  
  15.  
  16. print(list_manipulator([1,2,3], "remove", "end"))
  17. print(list_manipulator([1,2,3], "remove", "beginning"))
  18. print(list_manipulator([1,2,3], "add", "beginning", 20))
  19. print(list_manipulator([1,2,3], "add", "end", 30))
  20. print(list_manipulator([1,2,3], "remove", "end", 2))
  21. print(list_manipulator([1,2,3], "remove", "beginning", 2))
  22. print(list_manipulator([1,2,3], "add", "beginning", 20, 30, 40))
  23. print(list_manipulator([1,2,3], "add", "end", 30, 40, 50))
  24.  
Advertisement
Add Comment
Please, Sign In to add comment