Question: Am working on python and mongodb. I am trying to find names from a table with the by matching them with their phone no.s. The phone number is in a list which i created by getting the numbers from another table. Its working fine but I am getting the output being printed twice.

```
phone = ["9585507882","9542158582"]           
datum = []
for i in phone:
    cursor = db.name.find({"phone": i},{"_id": False})
    value = yield cursor.count()
    if value is 0:
        pass
    else:
        result = []
        for document in (yield cursor.to_list(length=100)):
            datum.append(document)
        print(datum)
        self.write(bson.json_util.dumps({"result": datum}))

```

My output is

```
{"result": [{"phone": "9585507882", "name": "Sanjay"}]}{"result": [{"phone": "9585509882", "name": "Sanjay"}, {"phone": "9542158582", "name": "Joe"}]}

```

can anyone help me out with this problem.

Answer: 
You're calling `self.write()` within the `for` loop, while you're still constructing `datum`. Move it outside so it runs only after all the data have been collected:

```
for i in phone:
    [...]
    if value == 0:  # don't use "is" for value comparison!
        pass
    else:
        [...]
        for document in (yield cursor.to_list(length=100)):
            datum.append(document)
        [...]
self.write(bson.json_util.dumps({"result": datum}))

```

Also, instead of 

```
if value == 0:
    pass
else:
    [do_stuff]

```

better do

```
if value:
    [do_stuff]

```

Also, what's the use of `result = []`? You're not doing anything with that list.
More simple way would be,

```
phone = ["9585507882","9542158582"]           
cursor = db.name.find({"phone": {"$in":phone}},{"_id": 0}).limit(100)       #$in will get you all document in phone array
result = list(cursor) #list(cursor) will convert cursor to python list
#you can then do anything you want

```