Python Forum

Full Version: Creating list of lists from generator object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a generator object 'results', which when looping through returns a list of dictionaries. I'm trying to convert this into a list of lists, so I can easily loop through and reference each value to be INSERTed into a database. I believe I'm having trouble because this is a generator object, how can I do this?

Consider the following:

def parse(results):
    for r in results:
        print(r)
Results in:
Output:
[{'id': '7229957054', 'repost_of': None, 'name': '1996 Acura Integra', 'url': 'https://monterey.craigslist.org/cto/d/salinas-1996-acura-integra/7229957054.html', 'datetime': '2020-11-12 14:37', 'last_updated': '2020-11-12 14:37', 'price': '$1,000', 'where': 'Salinas', 'has_image': True, 'geotag': None, 'deleted': False}, {'id': '7229839309', 'repost_of': None, 'name': '1990 Acura Integra GS', 'url': 'https://monterey.craigslist.org/cto/d/salinas-1990-acura-integra-gs/7229839309.html', 'datetime': '2020-11-12 11:31', 'last_updated': '2020-11-12 11:31', 'price': '$2,800', 'where': 'Salinas, Ca', 'has_image': True, 'geotag': None, 'deleted': False}]
my function:

def initialParse(results):
    rList = []
    for r in results:
        r_id = str(r['id'])
        r_name = str(r['name'])
        r_url = str(r['url'])
        r_datetime = str(r['datetime'])
        r_updated = str(r['last_updated'])
        r_price = str(r['price'])
        r_where = str(r['where'])
        iList = list(r_id + r_name + r_url + r_datetime + r_updated + r_price + r_where)
        rList.append(iList)
    print(rList)
returns:

Output:
[['7', '2', '2', '9', '9', '5', '7', '0', '5', '4', '1', '9', '9', '6', ' ', 'A', 'c', 'u', 'r', 'a', ' ', 'I', 'n', 't', 'e', 'g', 'r', 'a', 'h', 't', 't', 'p', 's', ':', '/', '/', 'm', 'o', 'n', 't', 'e', 'r', 'e', 'y', '.', 'c', 'r', 'a', 'i', 'g', 's', 'l', 'i', 's', 't', '.', 'o', 'r', 'g', '/', 'c', 't', 'o', '/', 'd', '/', 's', 'a', 'l', 'i', 'n', 'a', 's', '-', '1', '9', '9', '6', '-', 'a', 'c', 'u', 'r', 'a', '-', 'i', 'n', 't', 'e', 'g', 'r', 'a', '/', '7', '2', '2', '9', '9', '5', '7', '0', '5', '4', '.', 'h', 't', 'm', 'l', '2', '0', '2', '0', '-', '1', '1', '-', '1', '2', ' ', '1', '4', ':', '3', '7', '2', '0', '2', '0', '-', '1', '1', '-', '1'...]
Moving the rList.append() out one block gives a list in a list containing ALL entries... I need each result r in it's own list inside a list... like this:

Output:
[['id', 'name', 'url', 'datetime', 'lastupdated', 'price', 'where'], ['id', 'name', 'url', 'datetime', 'lastupdated', 'price', 'where'], ... ]
What am I doing wrong here? Or, is there a simpler way to convert this list of dictionaries from the generator object into a list containing each of the dictionaries values?
Very confusing. I am afraid that your description of the problem is contradictory.

However, your immediate problem is very simple and resides in line #11. To demonstrate what happens and what you probably want consider this:

>>> a = 'oh'
>>> b = 'my'
>>> a + b
'ohmy'
>>> list(a + b)        # equivalent of your row #11
['o', 'h', 'm', 'y']
>>> [a, b]             # this is what you probably want
['oh', 'my']
But of course, you don't need to create variables for just adding them into list not to mention that converting strings into strings doesn't make sense.

...and if 'results' is really generator which emits 'list of dictionaries' then applying initialParse() on it fails miserably as lists don't have keys
...and your sample output is not list of dictionary values, these are dictionary keys
...and dictionaries have dict.values() method