0
['#', 'vcrisan', '#sses', '#crusu', 'ALL', '#rpavlicek', 'oracle', '#vcrisan', 'dwilks,skumar', 'sjoshi,skekes', 'skekes', 'sdammalapati', 'sdammalapati']

I am trying to remove string with # in the list and if the string is like 'dwilks,skumar' will split it again and add to the string again removing the old one.

The condition I am using is working but for single time only

            for name in userslist:
                if '#' in name:
                    userslist.remove(name)
                if ',' in name:
                    newwlist=name.split(',')
                    userslist.remove(name)
                    for splittedname in newwlist:
                        userslist.append(splittedname)

            print (userslist)

Result:

['vcrisan', '#crusu', 'ALL', 'oracle', 'dwilks,skumar', 'skekes', 'sdammalapati', 'sdammalapati', 'sjoshi', 'skekes']

It's working for the first two # hash and not for the third one similarly for the comma case it working for the second value sjoshi,skekes only

NOTE: Please donot recommend re module

percusse
  • 3,006
  • 1
  • 14
  • 28
Tara Prasad Gurung
  • 3,422
  • 6
  • 38
  • 76

1 Answers1

2

This may help you,

 userslist = ['#', 'vcrisan', '#sses', '#crusu', 'ALL', '#rpavlicek', 'oracle', '#vcrisan', 'dwilks,skumar', 'sjoshi,skekes', 'skekes', 'sdammalapati', 'sdammalapati']

    pUserList = []
    for name in userslist:
       if not name.startswith('#'):  
          pUserList.extend(name.split(',')) 

    print (pUserList)
Kajal
  • 709
  • 8
  • 27
  • extend is used rather than my way of spliting and adding again. But where is it mentioned remove with # . So if its not found does the extend removes it – Tara Prasad Gurung May 19 '17 at 11:24
  • 1
    It will skip any entry starts with '#'. Split will return a list with two or more element in case of `,` and one element if the entry has no `,`. – Kajal May 19 '17 at 11:46
  • cool. Its checking for the one without # so others wont be taken and extending use working great with the one with comma, Thanks – Tara Prasad Gurung May 19 '17 at 11:49
  • NOTE:so the extends works even though comma is not found and performs the insertion for found item – Tara Prasad Gurung May 19 '17 at 11:51
  • 1
    Yes, A list with single entry ([element]) also will be considered as a list. So extend will work. – Kajal May 19 '17 at 11:51