Skip to content
Advertisement

How do I convert a list of strings to integers in Python [closed]

I need help on how to convert a list to integers, for example

lst = ["1", "2", "3"]

Desired output:

123

I think I explain myself, I do not speak English so I use a translator, I hope it is understood.

Advertisement

Answer

You need to do two things: 1. concatenate the elements of the array together into a single string, and 2. convert that string to a number.

You can do #1 with the join string method. Normally, you call join on some other string that you want to put in between the ones you’re joining, but since you don’t want one of those here, you can just use the empty string:

>>> lst=["1","2","3"]
>>> "".join(lst)
'123'

Since that’s still a string, not a numeric value, this is where step 2 comes in. You can convert it to an integer with the int function:

>>> int("".join(lst))
123
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement