Convert list to int Python | Example code

Use int() function to Convert 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 a 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))

Do comment if you have any doubts and suggestions on this Python list to int program.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.