Dynamically growing a python array when assigning to it

You need a dictionary

arr = {}
for x in xs:
    arr[x.index] = x

If all you are going to do is to build the dictionary, you can use dictionary comprehension like this

myDict = {x.index:x for x in xs}

Leave a Comment