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]]
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(4)
-
aws
(8)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(9)
-
conkeror
(2)
-
cygwin
(18)
-
datastructures
(15)
-
datetime
(3)
-
dell
(3)
-
django
(39)
-
emacs
(20)
-
files_directories
(10)
-
install_setup
(7)
-
javascript
(3)
-
keyboard
(6)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
preferences
(8)
-
processes
(3)
-
pyqt
(18)
-
python
(122)
-
ratpoison
(3)
-
regexes
(5)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(13)
-
ssh
(7)
-
subversion
(6)
-
twisted
(6)
-
ubuntu
(60)
-
urxvt
(5)
-
vxworks
(25)
-
webservices
(4)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan