Just think you have one of those attractive train sets. like
You need to add a train vehicle after the subsequent one. So you'd split up the train between index 1 and 2 and afterward connect it. The forward portion (front part) is everything from 0 to 1 and the subsequent part is everything from 2 till the end.
Fortunately, python has a truly pleasant slice syntax: x[i:j] implies slice from
i (inclusive) to j (exclusive). x[:j] implies slice from the front till j and x[i:] implies slice from i till the end.
So we can do
def add(lst, obj, index): return lst[:index] + [obj] + lst[index:]
Want to become a expert in python? Join the python course fast!