1
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%General%%Council%%Mandate%', u'%Regional%%Council%', u'%chair%%Label%', u'%chairman%%Title%', u'%chairman%', u'%chairperson%']


list2=[u'app General Council', u'people', u'people Regional Council Mandate', u'ppp General Council Mandate', u'Regional Council', u'chair Label', u'chairman Title', u'chairman', u'chairperson']

I wanted to change list1 into list2 to remove the % replace as " ". However, when i use following statement:

print [i.replace("%"," ") for i in list1]
[u' app  General  Council ', u' people ', u' people  Regional  Council  Mandate ', u' ppp  General  Council  Mandate ', u' Regional  Council ', u' chair  Label ', u' chairman  Title ', u' chairman ', u' chairperson ']

the result is not what i want. How to remove the space in the start and end of each element? so that i can get the result which is like list2.

4
  • Use the line i.replace("%","") instead of i.replace("%"," ") Commented Oct 28, 2016 at 4:15
  • i tried it. it cannot , it will join all the words together Commented Oct 28, 2016 at 4:17
  • 1
    As simple as [i.strip('%').replace('%%', ' ') for i in list1] Commented Oct 28, 2016 at 4:19
  • help(str.strip) will provide the documentation details necessary to solve your problem. Commented Oct 28, 2016 at 4:34

1 Answer 1

3

You may also try replacing "%%" with " " first and "%" with "" later. That will solve your problem.

In [18]: list2 = [i.replace("%%"," ") for i in list1]

In [19]: list2
Out[19]: 
[u'%app General Council%',
 u'%people%',
 u'%people Regional Council Mandate%',
 u'%ppp General Council Mandate%',
 u'%Regional Council%',
 u'%chair Label%',
 u'%chairman Title%',
 u'%chairman%',
 u'%chairperson%']

In [20]: list2 = [i.replace("%","") for i in list2]

In [21]: list2
Out[21]: 
[u'app General Council',
 u'people',
 u'people Regional Council Mandate',
 u'ppp General Council Mandate',
 u'Regional Council',
 u'chair Label',
 u'chairman Title',
 u'chairman',
 u'chairperson']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.