I’m from C and python is new to me. I know that both insert and append can do the same task of appending an element to a list.
Using insert:
x: list[int] = [1, 2, 3, 4]
x.insert(4,5)
for i in range(len(x)):
print(f"{x[i]} ", end = "")
print()
and using insert:
x: list[int] = [1, 2, 3, 4]
x.append(5)
for i in range(len(x)):
print(f"{x[i]} ", end = "")
print()
both will output 1 2 3 4 5
Say, my list keeps growing and growing. And let’s keep things fair with input and append with tracking the number of element so that insert() can access that index (for example, with list size of 100, I can do x.insert(size, num)
.
Which one would be better and efficient in time consumption, insert or append?