I have to extend list by adding one element. Pos indicates where I should insert new element and also which value should be inserted (the value of element in original list) F.e.
([1, 2, 3], 0) => [1, 1, 2, 3]
([1, 2, 3], 1) => [1, 2, 2, 3]
([1, 2, 3], 2) => [1, 2, 3, 3]
If pos is too high, I had to add 0 element to the end.
([1, 2, 3], 9) => [1, 2, 3, 0]
I have done so far:
def extend_list(numbers, pos):
if pos > len(numbers) - 1:
numbers.append(0)
return numbers
return numbers[:pos] + numbers[pos] + numbers[pos:]
But I get an error: TypeError: can only concatenate list (not "int") to list
What I'm doing wrong?