Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Remove adjacent items in-place

By: a guest on Aug 11th, 2010  |  syntax: Python  |  size: 0.38 KB  |  views: 47  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. nums = [1,1,1,2,2,3,3,3,6,5,5,1,1,1]
  2. def remove_adjacent(nums):
  3.     """Removes adjacent items by modifying "nums" in-place. Returns None!"""
  4.     i = 0
  5.     while True:
  6.         try:
  7.             if nums[i] == nums[i+1]:
  8.                 nums.pop(i)
  9.             else:
  10.                 i += 1
  11.         except IndexError:
  12.             break
  13. print nums
  14. remove_adjacent(nums)
  15. print nums
clone this paste RAW Paste Data