0

IM Creating list of str. how can remove the " in the list please?

['xxxxx', '', 'bb', "'errrr'"]

output desired:

 ['xxxxx', '', 'bb', 'errrr']

thank you

charles
  • 209
  • 3
  • 10
  • 1
    You don't want to drop the quotes from the list, but from an *element* of the list. How did the list get produced in the first place? For this specific example, `yourlist[3] = yourlist[3].strip("'")` would work – chepner Feb 27 '20 at 15:15
  • Why do you want to del the `"`?If you want to get the string element in the List,just use index to get the element,and it wouldn't show `"` and `'`. – jizhihaoSAMA Feb 27 '20 at 15:15
  • What have you tried so far? – TylerH Feb 27 '20 at 20:13

3 Answers3

2

You can replace() or strip() the single quote '

your_list = [s.replace("\'", "") for s in your_list]
your_list = [s.strip("\'", "") for s in your_list]

# e.g 
your_list = [s.replace("\'", "") for s in ['xxxxx', '', 'bb', "'errrr'"]]

Double quotes appear in print() because you have single quotes ' in one of your list elements

Example of the element is on 3rd position : 'errrr'.

Or if you want to strip double quotes " as well

your_list = [ s.strip("\'\"") for s in your_list]`
your_list = [ s.strip("\'\"") for s in ['xxxxx', '', '"bb"', "'errrr'"]]
print(your_list)
> ['xxxxx', '', 'bb', 'errrr']
alikhtag
  • 316
  • 1
  • 6
1
(your list name) = [i.replace("\'", '') for i in (your list name)]
Max Miller
  • 45
  • 5
0

your issue is related to your ' charcter from the string

you may see Using quotation marks inside quotation marks

for your example you can use str.replace method:

l = ['xxxxx', '', 'bb', "'errrr'"]
l[3] = l[3].replace("'", '')
l

output:

['xxxxx', '', 'bb', 'errrr']

or you can use str.strip:

l[3] = l[3].strip("'")
kederrac
  • 16,819
  • 6
  • 32
  • 55