I made a list of the following:

digits = list(str(number)) 

How to return the original number from the list?

    2 answers 2

    Like this:

     number = 15 number = list(str(number)) print(type(number)) #печатает <class 'list'> num = "" for n in number: num += n num = int(num) print(num, type(num)) #печатает 15 <class 'int'> 
    • Can you explain what's what? - Rostyslav Popov
    • We define the number variable, convert it to the list, then create an empty string and, in a loop, append characters from the string to it, and then cast this string to the int type. - zergon321
    • Thanks, but since I need more characters, I’ll write again - Rostyslav Popov

    It is possible so:

     number = int(''.join(number)) 

    Here ''.join(number) combines the list into a string, and int() makes a number from a string.

    • why does he merge into a string and how? or google .joyn? - Rostyslav Popov
    • The string method join combines (produces contact) of all the elements of the sequence passed to it as an argument, inserting the string for which the method was called between the elements. That is, imagine that we have a list lst = ["foo", "bar", "buzz"] , then if we do this in '+'.join(lst) , then 'foo+bar+buzz' will be printed. - Flowneee
    • 2
      for entertainment: reduce(lambda number, digit: number*10 + digit, map(int, digits)) - jfs