.

ISSUE | RESOLVED - Python | TypeError: can only concatenate list (not "int") to list


Error

TypeError: can only concatenate list (not "int") to list


If you see this error, you must be trying to concatenate an integer with a list. A sample snippet is :


1
2
3
4
5
6
7
8
9
def getbin(N, bn=[]):
    if (N == 0):
        return bn
    c = N % 2
    bn = c + bn
    return getbin(N / 2, bn)


print getbin(5)




Solution

c is an integer and bn is a list. To append a new value in list write it like this



bn = [c] + bn

No comments :

Post a Comment

Recent Posts