SHARE
TWEET

Remove adjacent items in-place

a guest Aug 11th, 2010 61 Never
  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
RAW Paste Data
Top