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

Posted in /  

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

Vinay Khatri
Last updated on November 8, 2024

    Similar to the strings, Python lists also support the concatenation operation between two list objects using the + operator. If we perform the + operator between two list objects, list_1 and list_2, it will return a new list object that will be a concatenation of list_1 and list_2.

    But if we perform the + operation between a list object and an integer value, we will receive the TypeError: can only concatenate list (not "int") to list Error.

    In this Python guide, we will talk about this error in detail and learn how to debug it. We will also walk through an example to demonstrate this error and solve the error in the solution section.

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

    Concatenation is an operation that joins two data objects into one. In Python, we can use the + operator between two strings, tuples, or lists objects, and it will return a new value of the same data type by joining the values of the two objects.

    Example

    # string concatenation 
    str_cat = "string1" + "string2"
    
    # list concatenation 
    list_cat = [1,2,3,4,5] + [6,7,8,9,10]
    
    # tuple concatenation 
    tuple_cat = (1,2,3,4,5,6) + (7,8,9,10,11)
    
    print(str_cat)  #string1string2
    print(list_cat)   #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    print(tuple_cat)  #(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)

    When we perform a concatenation operation between a list object and integer value, we receive the error TypeError: can only concatenate list (not "int") to list . The Error statement can be divided into two parts

    1. TypeError (Exception Type)
    2. can only concatenate list (not "int") to list

    TypeError

    TypeError is one of Python's standard exceptions. It is raised in a Python program when the interpreter finds an unsupported operation on or between two data objects. To perform a list concatenation, both operands must be a list. If any of the operands is of a different data type, we will receive the type error.

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

    can only concatenate list (not "int") to list is the error message. You will only encounter this error when you put the + operator between a list and an integer number in your Python program. This error message clearly specifies that Python can only concatenate list to list, not int to list.

    Example

    num = 4
    
    nums = [1,2,3]
    
    # add 4 to the list nums
    nums = nums + num
    
    print(nums)

    Output

    Traceback (most recent call last):
      File "main.py", line 7, in 
        nums = nums + num
    TypeError: can only concatenate list (not "int") to list

    Solution

    The solution of the above example depends on the situation or the logic of the program. The + symbol is also used for addition operations between two numbers. By looking at the above program, we can not tell what operation we want to perform.

    We might be thinking of adding value 4 to every element of the list nums , or we are just appending the value 4 at the end of the list using concatenation. If we want to add an integer number to every element of the list, we need to traverse through every element of the list and add the value.

    Example solution 1 (add the integer number to every element of the list)

    num = 4
    
    nums = [1,2,3]
    
    # add num to nums
    for i in range(len(nums)):
        nums[i] += num
    
    print(nums)

    Output

    [5, 6, 7]

    If we want to add a new element to the list object using + or concatenation operation, we should first consider using of python append method. Which is the most used list method to add a new element to the end of the list.

    Example Solution 2 (add the integer at the end of the list)

    # concatenate list and integer
    num = 4
    
    nums = [1,2,3]
    
    # add num to nums
    nums.append(num)
    
    print(nums)

    Output

    [1, 2, 3, 4]

    If you do not wish to use the append() method and want to add a new integer number to the list object using concatenation. There you first need to convert the integer object into a list by putting the square bracket around the number, then concatenate that converted list into the existing list.

    Example Solution 3 (add the integer at the end of the list)

    # concatenate list and integer
    num = 4
    
    nums = [1,2,3]
    
    # add num to nums
    nums = nums + [num]
    print(nums)

    Output

    [1, 2, 3, 4]

    Wrapping Up!

    The Python error "TypeError: can only concatenate list (not "int") to list" is raised when the Python interpreter finds the + operation between a list and an int object. Unlike other programming languages, Python does not support the + operation as an addition between list and int. Python list object treats the + operator as a concatenation operator and tries to concatenate the object on the right side of the operator.

    If you are still getting this error in your Python program, you can share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments