0

The original List is this

a=['="1111"', '="2222"', '="3333"', '="4444"']

I try this

b=[b.strip('=') for b in a]

Then i got this

[ ' "1111" ', ' "2222" ', ' "3333" ', ' "4444" ']

I have tried some way to remove outside ' ',but I still can not make it

I want to get

["1111","2222","3333","4444"]

1
  • 1
    Whatever you are doing, you should be managing your data in a less crazy way so that this is not necessary... You shouldn't have gotten the quotes there in the first place. Commented Sep 13, 2017 at 10:19

2 Answers 2

1

Technic -1 : Using strip():

>>> a=['="1111"', '="2222"', '="3333"', '="4444"']
>>> b=[b.strip('=').strip('"') for b in a]
>>> b
['1111', '2222', '3333', '4444']

Technic -2 : Using replace() :

>>> a=['="1111"', '="2222"', '="3333"', '="4444"']
>>> b=[b.replace("=","").replace('"',"") for b in a]
>>> b
['1111', '2222', '3333', '4444']

Above both Technics are suitable for simple and small list. If we follow the same approach for the list containing 1000 elements, it will iterate over 1000 elements. So for avoiding, you can follow Technic - 3:

>>> a=['="1111"', '="2222"', '="3333"', '="4444"']
>>> b = ','.join(a).replace("'","").replace('"',"").replace("=","").split(",")
>>> b
['1111', '2222', '3333', '4444']

or

>>> import re
>>> mystring = ','.join(a)
>>> re.sub('[^A-Za-z0-9,]+','',mystring).split(",")
['1111', '2222', '3333', '4444']
Sign up to request clarification or add additional context in comments.

Comments

0

str.strip() allows you to trim by multiple characters:

a = ['="1111"', '="2222"', '="3333"', '="4444"']
b = [i.strip('="') for i in a]
print(b)

The output:

['1111', '2222', '3333', '4444']

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.