SaltyCrane Blog — Notes on JavaScript and web development

How to convert a list of dictionaries to a list of lists in Python

UPDATE: See my new post, Tabular data structure conversion in Python for an updated method which handles non-rectangular data.

The functions below convert a rectangular list of dictionaries to a list of lists. Each dictionary in the list must have the same keys. Additionally, a list of keys is required as an input argument to specify the desired ordering of the columns in the returned list of lists. If this were not specified, the order of the columns would be unknown since items in a dictionary are unordered.

The converted list of lists can contain either a list of rows or a list of columns. The first two functions create a lists of rows; the last two create a list of columns. (I consider each dict in the list of dicts as a row, and all values for a given key as a column.)

I also compare the imperative/procedural approach to the declarative/functional approach. I like the declarative/functional approach because it is so concise, and, I believe, a little faster as well.

#!/usr/bin/python

def byrow_imper(lod, keylist):
    """Converts a list of dictionaries to a list of lists using the
    values of the dictionaries. Assumes that each dictionary has the
    same keys. 
       lod: list of dictionaries
       keylist: list of keys, ordered as desired
       Returns: a list of lists where the inner lists are rows. 
         i.e. returns a list of rows. """
    # imperative/procedural approach
    lol = []
    for row in lod:
        row2 = []
        for key in keylist:
            row2.append(row[key])
        lol.append(row2)
    return lol

def byrow_decl(lod, keylist):
    """Converts a list of dictionaries to a list of lists using the
    values of the dictionaries. Assumes that each dictionary has the
    same keys. 
       lod: list of dictionaries
       keylist: list of keys, ordered as desired
       Returns: a list of lists where the inner lists are rows. 
         i.e. returns a list of rows. """
    # declarative/functional approach
    return [[row[key] for key in keylist] for row in lod]

def bycol_imper(lod, keylist):
    """Converts a list of dictionaries to a list of lists using the
    values of the dictionaries. Assumes that each dictionary has the
    same keys. 
       lod: list of dictionaries
       keylist: list of keys, ordered as desired
       Returns: a list of lists where the inner lists are columns. 
         i.e. returns a list of columns. """
    # imperative/procedural approach
    lol = []
    for key in keylist:
        col = []
        for row in lod:
            col.append(row[key])
        lol.append(col)
    return lol

def bycol_decl(lod, keylist):
    """Converts a list of dictionaries to a list of lists using the
    values of the dictionaries. Assumes that each dictionary has the
    same keys. 
       lod: list of dictionaries
       keylist: list of keys, ordered as desired
       Returns: a list of lists where the inner lists are columns. 
         i.e. returns a list of columns. """
    # declarative/functional approach
    return  [[row[key] for row in lod] for key in keylist]

if __name__ == "__main__": 
    lod = [
        {'a':1, 'b':2, 'c':3},
        {'a':4, 'b':5, 'c':6},
        {'a':7, 'b':8, 'c':9},
        ]
    keylist = ['a', 'b', 'c']
    print byrow_imper(lod, keylist)
    print byrow_decl(lod, keylist)
    print bycol_imper(lod, keylist)
    print bycol_decl(lod, keylist)

Results:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Comments