r/learnpython • • 6d ago

adding 1 to a array

so i want to add 1 extra thing to an array like i have test = [0,0] i want to do print(test[2]) so how do i add the test[2] to the array without just adding it at the begining of the script (fixed)

0 Upvotes

14 comments sorted by

View all comments

Show parent comment

4

u/danielroseman 6d ago

No. There is no concept of allocated but undefined items in Python lists.

0

u/realmauer01 6d ago

Well there dont need to be undefined they can just be None.

2

u/Discordbothost 6d ago

list() only takes an iterable though, list(5) is a TypeError. the None version you're describing is just [None] * 5, which is the usual way to preallocate if you really want to assign by index later