NewBee
NewBee

Reputation: 23

How to remove single quote from a list in python

I have a list like this

res = ['-14.82381293', '-0.29423447', '-13.56067979', '-1.6288903']

I want to remove the single quotes ''. How can i achieve this on python.

I tried

res = [item.replace("'", '') for item in res]
print(res)

but it didn't work. any idea how?

Upvotes: 1

Views: 763

Answers (4)

Seyed_Ali_Mohtarami
Seyed_Ali_Mohtarami

Reputation: 1164

You can use this code:

res = ['-14.82381293', '-0.29423447', '-13.56067979', '-1.6288903']
res=map(str, res)
print ('[%s]' % ','.join(map(str, res))) 

This code is used for string or Integer or a decimal number for example:

res = ['-14.82381293', '-0.29423447', '-13.56067979', '-1.6288903','book','3234']

Upvotes: 0

vik
vik

Reputation: 51

In your case, the elements are stored as strings in the list res.
You have to convert those elements into numbers.

In this situation python's builtin map(...) function can be used.
Syntax of map:

variable_to_store_new_value = map(type, list_of_elements)

Above code will return a map object. Convert that into a list.

variable = list(map(type, list_of_elements))

Here type can be any valid python data type (str, int, float, etc.) or a function.

Solution to your problem

res = list(map(float, res))

Upvotes: 0

Vova
Vova

Reputation: 3537

what says float about it?

res = [float(item) for item in res]
print(res)

honestly, have not seen the answer in comments

Upvotes: 1

Shuvam Paul
Shuvam Paul

Reputation: 138

The quotes are coming because the numbers are stored as string data type.

Therefore to remove the quotes, you must convert them into float.

res = ['-14.82381293', '-0.29423447', '-13.56067979', '-1.6288903']
new_res=[]
for i in range(0,len(res)):
    new_res.append(float(res[i]))
print(new_res)

You can do this .

Upvotes: 0

Related Questions