Compute binary sum with Python 3 I tried to make a function able to compute the binary sum. Binary numbers are passed as list composed of 1 and 0, like [1,1,0,1], [0,0,1], etc.
It seems to work. I tried to improve the code, but I don't think  this is the best way to make it work. How can I improve this?

def check(value):
    if value%2 == 0: #check if the number can be divided by 2
        amount = int(value / 2) #if so, set the value to 0 and the amount as the number divided by 2
        value = 0
    elif value%2 != 0: #if not so, set the value to 0 and the amount as the previous even number divided by 2
        amount = int((value-1)/2)
        value = 1
    return value, amount #returns the 2 value.

def binary_sum(*args):
"""The program compute the binary sum of 2+ binary lists, like [1,1,1,1] + [0,0,0,1]"""
    i = -1 #control variable
    result = [] #list that will be filled by results' binary numbers
    value = 0 
    amount = 0 #amount carried over
    binaryLength = [] #list of all args length
    for x in args:  binaryLength.append(len(x)) #get the length of each args elements
    maxLength = max(binaryLength) #get the max length among args elements
    for x in args:
        if len(set(binaryLength)) != 1: #checks if all elements have the same length
            delta = maxLength - len(x) - 1 #if not so, it adds as many zeros as delta value indicates
            while (delta) &gt;= 0:
                x[:0] = [0]
                delta -= 1
    while i &gt;= -maxLength: #starts the sum from the end.
        for j in args: value += j[i] #get the sum of all the last list element( for each arg)
        value += amount 
        value, amount = check(value) #uses binary sum properties: it returns a value between 0 and 1, and the amount, an integer 
        result.insert(i, value) #inserts the value at the beginning of the list
        i-=1 # needed to iterate the process
        value = 0 #resets the value
    if amount != 0: #At the end, if there's amount it inserts it at the beginning of result list: 
        while amount not in [0,1]: #if is not equal to 0 or 1 converts the int amount in binary number 
            value = amount
            value, amount = check(value)
            result.insert(i, value)
            i-=1
        result.insert(i, amount) #if is equal to 1 inserts it at the start of the list.
    return result #returns the result as list.
You've got way too much code.  You are not using the built-in tools Python provides to solve this problem, making your job much, much harder.



In your check(value) function, you are checking:

if value % 2 == 0:
    # ...
elif value % 2 != 0:
    # ...


If value % 2 == 0 is False, then value % 2 != 0 is guaranteed to be True.  There is no need for the elif ...:; you could simply use an else: clause.

In both cases, you assign amount = int( ... / 2).  Python has built-in integer division operator, //, so these statements could become simply amount = ... // 2.  In the second case, because value is odd, you divide value - 1 by two.  This "subtract 1" is unnecessary.  Using integer division (or dividing and then casting to an integer) will truncate the result to an integer.

Afterwards, in the first case, when value % 2 is zero, you assign value = 0, where as in the second case, when value % 2 is one, you assign value = 1.  You could simply assign value = value % 2.

def check(value):
    amount = value // 2
    value = value % 2
    return value, amount


Or slightly shorter:

def check(value):
    return value % 2, value // 2


Or even better, use the built-in function divmod(a,b) which does the same thing.  But you will have to swap the variables you assign to, because divmod(a,b) returns the result of the division first, and the modulo remainder second:

amount, value = divmod(value, 2)




In your binary_sum(*args) function, you are given a variable number of lists, and you want to take one element from each list, then take the next element from each list, and then take the next element from each list, and so on.  In Python, this is the zip() function.  (Note: "zip" is short for "zipper", not zip-compression.)

for addends in zip(*args):
    # ...


Except we have a wrench in the works; you want to start from the end and work towards the front.  That is hard.  It is easier if each list is reversed(), so starting from the start is starting from what was the end.  We can then zip() those reversed() lists together:

for addends in zip(*map(reversed, args)):
    # ...


Except we have another wrench in the works; the lists aren't all the same length, and zip stops when any of the lists runs out of items.  You need to add 0's at the start, so they are the same length.  Except we've reversed the lists, so we need to add 0's to the end.  That's easy.  We just need itertools.zip_longest(*args, fillvalue=0) to provide the extra 0's.

from itertools import zip_longest

for addends in zip_longest(*map(reversed, args), fillvalue=0):
    # ...


Now addends is first the 1's column digits, then the 2's column digits, then the 4's column digits, and so on.  We can just sum the addends together, along with any carry from the previous column:

digits = []
carry = 0
for addends in zip_longest(*map(reversed, args), fillvalue=0):
    total = sum(addends) + carry
    carry, digit = divmod(total, 2)
    digits.append(digit)


And then, add the carry bits to end once you've run out of columns:

while carry &gt; 0:
    carry, digit = divmod(carry, 2)
    digits.append(digit)


Finally, since we've generated the result in the wrong order, you'll have to reverse it again, so the most significant digit is first.

from itertools import zip_longest

def binary_sum(*args):
    digits = []

    carry = 0
    for addends in zip_longest(*map(reversed, args), fillvalue=0):
        total = sum(addends) + carry
        carry, digit = divmod(total, 2)
        digits.append(digit)

    while carry &gt; 0:
        carry, digit = divmod(carry, 2)
        digits.append(digit)

    return digits[::-1]


A few tests:

&gt;&gt;&gt; binary_sum([1,1,1,0])
[1, 1, 1, 0]
&gt;&gt;&gt; binary_sum([1,1,1,0], [1,0])
[1, 0, 0, 0, 0]
&gt;&gt;&gt; binary_sum([1,1,1,1], [1])
[1, 0, 0, 0, 0]
&gt;&gt;&gt; binary_sum([1,1,1,1], [0,0,0,1])
[1, 0, 0, 0, 0]
&gt;&gt;&gt; binary_sum([1,1,1,1], [1,0,0,0])
[1, 0, 1, 1, 1]
&gt;&gt;&gt; binary_sum([1], [1], [1], [1], [1])
[1, 0, 1]