jcmetz21 jcmetz21 - 11 months ago 58
Python Question

List Comprehension Glitch

I'm trying to query a MongoDB database and throw the two sets of results ('_id' and 'Team') into two separate lists.

import pymongo

client = pymongo.MongoClient('localhost:27017')
db = client['db_name']

query = {'Team': {'$exists': 1}}
projection = {'_id': 1, 'Team': 1}

data = db['collection_name'].find(query, projection) # line 9

id_list = [value for dict in data for key, value in dict.iteritems() if key == '_id']
teams_list = [value for dict in data for key, value in dict.iteritems() if key == 'Team']

print id_list
print teams_list


For the code above, the 'id_list' is as expected but 'teams_list' is empty. When I put 'teams_list' before 'id_list' I get the expected 'teams_list' output and 'id_list' is empty. And when I repeat the data call (line 9) in between the two list comprehensions I get the expected output for both lists.

Any idea why this is happening?

Answer Source

You need to define your data as:

data = list(db['collection_name'].find(query, projection))

As find() returns the generator. Once you iterate the values, those are lost. You need to store them as list. Here list() does that i.e. stores the items returns by generator as list.

Instead of iterating the list twice, better way will be two do it single loop as:

id_list, teams_list = [], []

#   v  `dict` is in-built data type, you should not be using it as variable
for d in data:
    for key, value in d.iteritems():
        if key == '_id':
        elif key == 'Team':

Refer Generator wiki for more information related to generators