Saykat
Saykat

Reputation: 124

How to turn a list of integers to a single integer?

I have list that contains only integers. I want it to be a full single integer. Such that I have list like this:

[500, 400, 300, 200, 100]

I want it to be like this:

500400300200100

Where is what I have tried:

ls = [500, 400, 300, 200, 100]
new_ls = []
for i in range(len(ls)):
    new_ls.append(str(ls[i]))

integer = int(''.join(new_ls))
print(integer)

Note that: join() doesn't work for list which contains integer(at least this is what I know). For that I am first turning the list to string then joining. Is there any shorter way for this?

Upvotes: 0

Views: 239

Answers (2)

luigigi
luigigi

Reputation: 4215

Try:

integer = int(''.join([str(x) for x in ls]))

Upvotes: 2

Dani Mesejo
Dani Mesejo

Reputation: 61910

You could do:

lst = [500, 400, 300, 200, 100]
result = int(''.join(list(map(str, lst))))
print(result)

Output

500400300200100

Upvotes: 2

Related Questions