Skip to content

Convert list to int Python | Example code

Use the int() function to Convert the list to int in Python. This method with a list comprehension returns one integer value that combines all elements of the list.

Example convert list to int Python

Simple example code converting [1, 2, 3] into one integer in 123. Use list comprehension to construct a new list with str(int) applied to all elements.

l = [1, 2, 3]

s = [str(integer) for integer in l]
a_string = "".join(s)

res = int(a_string)

print(res)
print(type(res))

Output:

Convert list to int Python

How to convert all elements of a list to int in Python

l = [1, 2, 3]

res = [int(item) for item in l]

print(l)
print(type(l))

Using the map function:

str_list = ['1', '2', '3', '4']
int_list = list(map(int, str_list))

print(int_list)  
# Output: [1, 2, 3, 4]

Comment if you have any doubts or suggestions on this Python list to int program.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

1 thought on “Convert list to int Python | Example code”

  1. Hola, me sirvio este metodo pero hay un error.

    l = [1, 2, 3]
    res = [int(item) for item in l]
    print(l)
    print(type(l))

    En esta parte se tiene que cambiar la variable en los print de i a res, seria algo asi:

    print(res)
    print(type(res))

    Perdon por las tildes, no se como configurar el teclado xd

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading