Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a list of dictionaries into several lists of dictionaries

Tags:

I've been whacking away at this for a while to no avail... Any help would be greatly appreciated.

I have:

[{'event': 0, 'voltage': 1, 'time': 0}, {'event': 0, 'voltage': 2, 'time': 1}, {'event': 1, 'voltage': 1, 'time': 2}, {'event': 1, 'voltage': 2, 'time': 3}, {'event': 2, 'voltage': 1, 'time': 4}, {'event': 2, 'voltage': 2, 'time': 5}, ...] 

and I want to split that list of dictionaries up per event like this (there can be arbitrarily many events):

list0 = [{'event': 0, 'voltage': 1, 'time': 0}, {'event': 0, 'voltage': 2, 'time': 1}]  list1 = [{'event': 1, 'voltage': 1, 'time': 2}, {'event': 1, 'voltage': 2, 'time': 3}]  list2 = [{'event': 2, 'voltage': 1, 'time': 4}, {'event': 2, 'voltage': 2, 'time': 5}]  listN = ... 
like image 701
thenickname Avatar asked Nov 03 '10 21:11

thenickname


People also ask

Can you split a list of lists in Python?

To split the elements of a list in Python: Use a list comprehension to iterate over the list. On each iteration, call the split() method to split each string. Return the part of each string you want to keep.

How do you break nested dictionaries in Python?

To delete an item stored in a nested dictionary, we can use the del statement. The del statement lets you delete an object. del is written like a Python break statement, on its own line, followed by the item in the dictionary that you want to delete.

How do you split a list into smaller lists in Python?

You could use numpy's array_split function e.g., np. array_split(np. array(data), 20) to split into 20 nearly equal size chunks.

Can you nest a dictionary in a list?

Today we'll be talking about the nested list and dictionary. Nested lists and dictionaries are often used to better structure your data. Theoretically there are no limits to the depth of nesting and you can have a list inside another list, which is inside yet another list, which itself also is inside another list, etc.


2 Answers

use defaultdict

import collections  result = collections.defaultdict(list)  for d in dict_list:     result[d['event']].append(d)  result_list = result.values()        # Python 2.x result_list = list(result.values())  # Python 3 

This way, you don't have to make any assumptions about how many different events there are or if there are any events missing.

This gives you a list of lists. If you want a dict indexed by event, I would probably use dict(d) if you plan on doing any random access.

As far as constructing a bunch of individual lists, I think that that's a bad idea. It will necessitate creating them as globals or using eval (or getting hacky in some other way) unless you know exactly how many there are going to be which you claim not to. It's best to just keep them in a container.

like image 56
aaronasterling Avatar answered Nov 10 '22 18:11

aaronasterling


This one is O(n log n) because of the sort, but I wouldn't worry too much unless there are a lot of items in the list.

It the list is already sorted by event, you can skip the sort of course.

>>> from operator import itemgetter >>> from itertools import groupby >>> d=[{'event': 0, 'voltage': 1, 'time': 0}, ... {'event': 0, 'voltage': 2, 'time': 1}, ... {'event': 1, 'voltage': 1, 'time': 2}, ... {'event': 1, 'voltage': 2, 'time': 3}, ... {'event': 2, 'voltage': 1, 'time': 4}, ... {'event': 2, 'voltage': 2, 'time': 5}] >>> groupby(sorted(d, key=itemgetter('event')), key=itemgetter('event')) <itertools.groupby object at 0xb78138c4> >>> for x in _: ...   print x[0], list(x[1]) ...  0 [{'time': 0, 'event': 0, 'voltage': 1}, {'time': 1, 'event': 0, 'voltage': 2}] 1 [{'time': 2, 'event': 1, 'voltage': 1}, {'time': 3, 'event': 1, 'voltage': 2}] 2 [{'time': 4, 'event': 2, 'voltage': 1}, {'time': 5, 'event': 2, 'voltage': 2}] 
like image 23
John La Rooy Avatar answered Nov 10 '22 19:11

John La Rooy