I have a list of object, each object contains a unique ID. The current list is like so:
[{"id": 3}, {"id": 5}, {"id": 8}]
I want to change the indexes a bit - instead of the dictionary with id = 3 to be on index 0, I want the dictionary with id = 3 to be on index 3.
I have tried:
list = []
for item in items:
list[item["id"]] = item
but that gives me indexing error. Then I tried to do the following:
list = []
for item in items:
list.insert(item["id"], item)
which just acts like a regular append (does not insert at specified index).
I have a last ditch attempt to do this on the front end when I am getting the response from the API but that defeats the whole purpose of reducing the amount of looping I do on the client side. The reason being is that the server can knock out these loops in like 50 milliseconds and on the client side it takes up to 300 milliseconds since it runs in the browser.
0. You can either (a) use a dictionary instead of a list, or (b) pre-fill the list will a specific number ofNonevalues.