Search code examples
pythonlistmap-function

i have a python list, using map function is omitting the first zero of the list


I have this code in python, when I print the last line, it is giving an output "11100101100". I'm expecting the output,"011100101100". Notice that the output starts with 1 and not 0. although the variable gamma_sum_list is a list containing 12 digits and its starts with 0. The function somehow deletes the first zero automatically. The following is the exact gamma_sum_list:

def convert(list)
   res = int("".join(map(str,list)))
   return res
print(convert(gamma_sum_list))

Input:

[0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0]

Expected Output:

011100101100

Actual Output :

11100101100

Solution

  • Your issue is caused by converting the result of the join operation to an integer. Integers do not have leading zeroes. If you remove the int function you'll get a string with the leading zero you're after.

    gamma_sum_list = [0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0]
    
    def convert(my_list):
    res = "".join(map(str,my_list))
    return res
    
    print(convert(gamma_sum_list))
    

    Output:

    011100101100