Reputation: 1303
original_list = [[a,1],[a,2],[a,3],[b,5],[a,4],[b,6],[c,7],[c,8],[c,9],[c,0]]
I have list like the original_list. How can I group them base on the characters like so?
dict = {'a': [1,2,3,4], 'b':[5,6], 'c':[7,8,9,0]}
Upvotes: 1
Views: 62
Reputation: 10213
Normal List iteration and Dictionary
>>> original_list = [["a",1],["a",2],["a",3],["b",5],["a",4],["b",6],["c",7],["c",8],["c",9],["c",0]]
>>> original_list
[['a', 1], ['a', 2], ['a', 3], ['b', 5], ['a', 4], ['b', 6], ['c', 7], ['c', 8], ['c', 9], ['c', 0]]
>>> resut = {}
>>> for i in original_list:
... if i[0] in resut:
... resut[i[0]].append(i[1])
... else:
... resut[i[0]] = [i[1]]
...
>>> resut
{'a': [1, 2, 3, 4], 'c': [7, 8, 9, 0], 'b': [5, 6]}
>>>
By collections.defaultdict method:
This will remove inner if loop statements.
>>> import collections
>>> result = collections.defaultdict(list)
>>> result
defaultdict(<type 'list'>, {})
>>> for i in original_list:
... result[i[0]].append(i[1])
...
>>> result
defaultdict(<type 'list'>, {'a': [1, 2, 3, 4], 'c': [7, 8, 9, 0], 'b': [5, 6]})
>>>
Do not use reserved keyword as variable names
dict
is inbuilt dictionary type variable.
Demo:
>>> dict
<type 'dict'>
>>> a = dict()
>>> a
{}
>>> dict = {"a":1}
>>> b = dict()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not callable
Upvotes: 1
Reputation: 90889
According to timing results (Updated below in the answer) fastest method could be using collections.defaultdict
. Example -
from collections import defaultdict
result_dic = defaultdict(list)
for a,b in original_list:
result_dic[a].append(b)
Demo -
>>> original_list = [['a',1],['a',2],['a',3],['b',5],['a',4],['b',6],['c',7],['c',8],['c',9],['c',0]]
>>>
>>> from collections import defaultdict
>>> result_dic = defaultdict(list)
>>> for a,b in original_list:
... result_dic[a].append(b)
...
>>> result_dic
defaultdict(<class 'list'>, {'b': [5, 6], 'c': [7, 8, 9, 0], 'a': [1, 2, 3, 4]})
Another method can be to use itertools.groupby
for this. Example -
from itertools import groupby
result_dict = {key:list(b for a,b in group) for key,group in groupby(sorted(original_list),key=lambda x:x[0])}
Demo -
>>> original_list = [['a',1],['a',2],['a',3],['b',5],['a',4],['b',6],['c',7],['c',8],['c',9],['c',0]]
>>> from itertools import groupby
>>> result_dict = {key:list(b for a,b in group) for key,group in groupby(sorted(original_list),key=lambda x:x[0])}
>>> result_dict
{'b': [5, 6], 'c': [0, 7, 8, 9], 'a': [1, 2, 3, 4]}
Timing results -
In [21]: %paste
def func1():
original_list = [['a',1],['a',2],['a',3],['b',5],['a',4],['b',6],['c',7],['c',8],['c',9],['c',0]]
return {key:list(b for b in group) for key,group in groupby(sorted(original_list),key=lambda x:x[0])}
def func2():
original_list = [['a',1],['a',2],['a',3],['b',5],['a',4],['b',6],['c',7],['c',8],['c',9],['c',0]]
result_dic = defaultdict(list)
for a,b in original_list:
result_dic[a].append(b)
return result_dic
def func3():
original_list = [['a',1],['a',2],['a',3],['b',5],['a',4],['b',6],['c',7],['c',8],['c',9],['c',0]]
result = {}
for x in original_list: result.setdefault(x[0], []).append(x[1])
return result
## -- End pasted text --
In [22]: %timeit func1()
The slowest run took 4.56 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 12.1 µs per loop
In [23]: %timeit func2()
100000 loops, best of 3: 3.82 µs per loop
In [24]: %timeit func3()
The slowest run took 4.77 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 4.31 µs per loop
Upvotes: 4
Reputation: 5533
Use one list comprehension, only for short list, given the O(n*n)
complexity.
>>> a = [['a',1],['a',2],['a',3],['b',5],['a',4],['b',6],['c',7],['c',8],['c',9],['c',0]]
>>> {k:[v for k1, v in a if k == k1] for k, v in a}
{'a': [1, 2, 3, 4], 'c': [7, 8, 9, 0], 'b': [5, 6]}
Upvotes: 2
Reputation: 19733
Try this: pythonic way
>>> import itertools
>>> { x: map(lambda x:x[1], y) for x,y in itertools.groupby(sorted(original_list, key=lambda x:x[0]), key=lambda x:x[0])}
{'a': [1, 2, 3, 4], 'c': [7, 8, 9, 0], 'b': [5, 6]}
Upvotes: 0
Reputation: 6957
In [48]: result = {}
In [49]: for x in original_list: result.setdefault(x[0], []).append(x[1])
In [50]: result
Out[50]: {'a': [1, 2, 3, 4], 'b': [5, 6], 'c': [7, 8, 9, 0]}
Upvotes: 3