List concatenation error. Motive is to double the index value from argument

def double_index(lst, index):
  if index > len(lst)-1:
    return lst
  else:
    lst_start = lst[0:index]
    lst_end = lst[index+1:]
    lst_middle = lst[index]*2
    return lst_start + list(lst_middle) + lst_end

print(double_index([3, 8, -10, 12], 2))
Traceback (most recent call last):
  File "script.py", line 11, in <module>
    print(double_index([3, 8, -10, 12], 2))
  File "script.py", line 9, in double_index
    return lst_start + lst(lst_middle) + lst_end
TypeError: 'list' object is not callable
lst(lst_middle)

what are you trying to do with this code? I think you need to make lst_middle a list and the easiest way to do this is with [] so it will be [lst_middle].
so your return statement should be return lst_start + [lst_middle] + lst_end

1 Like

There is reason why I did this:
return lst_start + list(lst_middle) + lst_end
earlier my code was like this:

`def double_index(lst, index):
  if index > len(lst)-1:
    return lst
  else:
    lst_start = lst[0:index]
    lst_end = lst[index+1:]
    lst_middle = lst[index]*2
    return lst_start + lst_middle + lst_end

print(double_index([3, 8, -10, 12], 2))``

Got this error, where it says to concatenate lists, I thought the list_middle converted to an integer because I multiplied by 2

return lst_start + lst_middle + lst_end
TypeError: can only concatenate list (not "int") to list

I want to ask Why it got converted to integer when I multiplied the list by 2??
lst_middle = lst[index]*2

and even after adding list(lst[index]*2)`, it didn’t convert

sorry its not
lst(lst_middle)
its
list(lst_middle)

Its a typo mistake. but the error did come in my computer

You can’t turn a single integer into a list. You can add a single integer to a list, and you can create a list with a single integer in it, but you can’t split an integer up into list elements. Have you considered using .append() instead of concatenating lists?

2 Likes

It was already an integer you didn’t convert it when you multiply it by 2.

    lst_start = lst[0:index] # slice notation
    lst_end = lst[index+1:] # slice notation
    lst_middle = lst[index]*2 #select an element

lst_star and lst_end you are using slice notation here which needs to specify the start and the end and it will return a new array with this slice.

BUT in lst_middle = lst[index] you have just selected an element from the array, you didn’t use the slice notation. And, that’s why it just returns the selected element to you with its data type, and because this element was a number, lst[index] returns a number.

=============================
If you want to use slice notation with the lst_middle to return just an array with one element, you can:

lst_middle = lst[index:index+1] #returns a list with one element!
# now it's a list with one element [element] to select it use [0]
lst_middle[0] = lst_middle[0] * 2 #double this element  

now you have a list so, you can return the concentrate directly

return lst_start + lst_middle + lst_end

========
or you can leave lst_middle as a number and double it then make a list with this element as I mentioned in the first comment

# make a list with only one element [lst_middle]
return lst_start + [lst_middle] + lst_end

and as @codeneutrino mentioned,

so you can’t use list(lst_middle). also as he mentioned you can use .apend() to make a new list instead of concatenating lists. choose whatever you want.

2 Likes