Question: I have a list of user `user_list=["5c471d607f","5c63e9e46a","5c63e9e44e"]` I am trying to insert it into mongodb to insert it inro mongodb my code is:

```
for i in range(0,len(user_list)):
    collection_4.update_one({'user_id':ObjectId(user_list[i])},{'$set':{'products': temp_list }},upsert=True)

This is giving me error, TypeError: list indices must be integers or slices, not ObjectId

```

While i am able to insert it when i tried:

```
collection_4.update_one({'user_id':ObjectId("5c471d607f")},{'$set':{'products': temp_list }},upsert=True)

```

What is the problem with my code ?

Answer: 
You must have another error in your script somewhere. I can reproduce your error by doing the following:

```
from bson import ObjectId
l = ['a', 'b', 'c']
l[ObjectId('5c91ec870000000000000000')]

```

gives:

```
TypeError: list indices must be integers or slices, not ObjectId

```

Are you sure you're not attempting to slice a list anywhere using an `ObjectId`, rather than using an index?

You might need to print more code because your current example seems to be ok.

One other improvement that @DirtyBit picked up, you can use `enumerate` to iterate over a list whilst tracking the index:

```
for i, item in enumerate(user_list):
    collection_4.update_one({'user_id':ObjectId(item)},{'$set':{'products': temp_list }},upsert=True)

```

But then again, you don't require the index, so you could also just do:

```
for item in user_list:

```
try this

```
for i in range(0,len(user_list)):
    myid = user_list[i]
    collection_4.update_one({'user_id':str(myid)},{'$set':{'products': temp_list }},upsert=True)

```