Talha Farid
Talha Farid

Reputation: 23

How to remove the single quotation mark from a list in Python

I am trying to remove the single quotation marks from this list:

list = ['amazed, 10']

and convert it to

list = ['amazed', 10]

I used list= [x.strip('') for x in list] but it does not work.

Is there a workaround?

Thanks in advance!

Upvotes: 2

Views: 2123

Answers (5)

neehari
neehari

Reputation: 2612

Try:

lst = ['amazed, 10']

lst = [int(i) if i.isdigit() else i for i in lst[0].replace(',', '').split()]

Upvotes: 0

Lior Magen
Lior Magen

Reputation: 1573

First you need to split your list to two elements. Next, strip white space and than convert the second element (a string of number) to number (Iv'e converted it to integer but you can convert to float or whatever).

ll = ['amazed, 10']
ll = ll[0].split(",")
ll[1] = int(ll[1].strip())

Upvotes: 0

Akhil Reddy
Akhil Reddy

Reputation: 391

as it is a single item in the list, u can split the string using split() method.

list=list[0].split(', ')

it will give two separate strings.

Upvotes: 0

Harsha Biyani
Harsha Biyani

Reputation: 7268

You can try:

>>> l = ['amazed, 10']
>>> l = l[0].split(", ")
>>> l
['amazed', ' 10']

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

You need to split but not strip, as your list contains a single string 'amazed, 10' that expected to be split into 2 items - 'amazed' and 10:

lst = ['amazed, 10']
lst = [int(i) if i.isdigit() else i for i in lst[0].split(', ')]
print(lst)

The output:

['amazed', 10]

Upvotes: 3

Related Questions