-5

I have a list as :

a=[u'hello',u'well',u'1024']

I want to have it as :

a=[u'hello',u'well',1024]

So please suggest how will i have it.

anamika email
  • 327
  • 9
  • 21
  • Is all values where you want to *remove the quotes* , just normal numbers? or decimals? – Anand S Kumar Aug 20 '15 at 10:35
  • `int(a[2])` what have you tried? if you've defined the list like that then just remove the characters that make it a string... – Sayse Aug 20 '15 at 10:37
  • 1
    None of those values have quotes in them, unless you're referring to values in the Python syntax tree. – Chris Martin Aug 20 '15 at 10:59
  • You might want to chek if the string represents an int. See http://stackoverflow.com/questions/1265665/python-check-if-a-string-represents-an-int-without-using-try-except, The code from this answer can be modified to check for floats as well. When you are sure the string can be converted, convert it to int (or float) – Maarten Aug 20 '15 at 11:01

1 Answers1

1

Python Map and a conversion function with try catch will do

lst=[u'hello',u'well',u'1024']

def conversion(value):
    try:
        return int(value)
    except Exception:
        return value


map(conversion,lst)
[u'hello', u'well', 1024]
Community
  • 1
  • 1
The6thSense
  • 8,103
  • 8
  • 31
  • 65