Removing single quotes from a python list item

Actually a pretty simple question: I have a python list, for example:

['1','2','3','4'] 

Just wondering how can I strip these single quotes? I want [1,2,3,4]

+4
source share
3 answers

Currently, all the values โ€‹โ€‹in your list are strings, and you want them to be integers, here are the two easiest ways to do this:

 map(int, your_list) 

and

 [int(value) for value in your_list] 

See map () documentation and a list of concepts for more information.

If you want to leave items in your list as strings, but display them without single quotes, you can use the following:

 print('[' + ', '.join(your_list) + ']') 
+18
source

If this is the actual python list and you want an int instead of strings, you can simply:

 map(int, ['1','2','3','4']) 

or

 [int(x) for x in ['1','2','3','4']] 
+5
source

try it

 [int(x) for x in ['1','2','3','4']] [1, 2, 3, 4] 

and for safe use you can try

 [int(x) if type(x) is str else None for x in ['1','2','3','4']] 
+4
source

Source: https://habr.com/ru/post/1385170/


All Articles