Skip to content Skip to sidebar Skip to footer

Remove Single Quotes From Python List Item

Actually quite simple question: I've a python list like: ['1','2','3','4'] Just wondering how can I strip those single quotes? I want [1,2,3,4]

Solution 1:

Currently all of the values in your list are strings, and you want them to integers, here are the two most straightforward ways to do this:

map(int, your_list)

and

[int(value) for value in your_list]

See the documentation on map() and list comprehensions for more info.

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

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

Solution 2:

If that's an actual python list, and you want ints instead of strings, you can just:

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

or

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

Solution 3:

Try this

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

and to play safe you may try

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

Post a Comment for "Remove Single Quotes From Python List Item"