By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Help with removing sequences from list

P: 3
hi all,
I am trying to remove the following from mylist
mylist=[28, 227, 28, 227, 0, 255, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 68, 187, 68, 187, 7, 248, 7, 248, 0, 0, 0, 0, 0, 0, 0, 0, 70, 185, 70, 185, 2, 253, 2, 253]

I only want to remove the zeros that are being repeated every 8th,24th item(in bold). I tried a few methods like using filter and all it worked but it also removed the zeros which i intend to keep like the 4th and 6th item in mylist...Is there a good way to do this?...thks
Jul 3 '09 #1
Share this Question
Share on Google+
2 Replies


P: 32
Expand|Select|Wrap|Line Numbers
  1. mylist=[28, 227, 28, 227, 0, 255, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 68, 187, 68, 187, 7, 248, 7, 248, 0, 0, 0, 0, 0, 0, 0, 0, 70, 185, 70, 185, 2, 253, 2, 253]
  2. removeIndexList = []
  3. for i in xrange(len(mylist)-1):
  4.     if mylist[i] == 0 and mylist[i+1] == 0:
  5.         removeIndexList.append(i)
  6.         removeIndexList.append(i+1)
  7. myfinallist = [eachElement for i, eachElement in enumerate(mylist) if i not in removeIndexList]
  8.  
  9.  
  10.  
  11.  
Jul 3 '09 #2

P: 3
tried it and it works...cool
guess have lots to learn!
thks!
Jul 3 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.