A pandas solution:
Setup:
import pandas as pd
a = [0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 4, 4]
b = pd.np.random.randint(0, 100, len(a)).tolist()
>>> b
Out[]: [28, 68, 71, 25, 25, 79, 30, 50, 17, 1, 35, 23, 52, 87, 21]
df = pd.DataFrame(columns=['Group', 'Value'], data=list(zip(a, b))) # Create a dataframe
>>> df
Out[]:
Group Value
0 0 28
1 0 68
2 0 71
3 1 25
4 1 25
5 1 79
6 1 30
7 1 50
8 2 17
9 2 1
10 2 35
11 3 23
12 4 52
13 4 87
14 4 21
Solution:
>>> df.groupby('Group').Value.apply(list).to_dict()
Out[]:
{0: [28, 68, 71],
1: [25, 25, 79, 30, 50],
2: [17, 1, 35],
3: [23],
4: [52, 87, 21]}
Walkthrough:
- create a
pd.DataFrame from the input lists, a is called Group and b called Value
df.groupby('Group') creates groups based on a
.Value.apply(list) gets the values for each group and cast it to list
.to_dict() converts the resulting DataFrame to dict
Timing:
To get an idea of timings for a test set of 1,000,000 values in 100,000 groups:
a = sorted(np.random.randint(0, 100000, 1000000).tolist())
b = pd.np.random.randint(0, 100, len(a)).tolist()
df = pd.DataFrame(columns=['Group', 'Value'], data=list(zip(a, b)))
>>> df.shape
Out[]: (1000000, 2)
%timeit df.groupby('Group').Value.apply(list).to_dict()
4.13 s ± 9.29 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
But to be honest it is likely less efficient than itertools.groupby suggested by @RomanPerekhrest, or defaultdict suggested by @Ajax1234.
if list_a[i] in d.keysandd[list_a[i]] = [list_b[i]]? Please post exactly the code you've tried, preferably using copy+paste (if available on your platform).ifmeans iflist_a[i]is already a key in the dictionary, then addlist_b[i]into the dictionary under keylist_a[i], whereaselsemeans that if not, addlist_b[i] to the new keylist_a[i]` as list. Hope it helps.list_[a] in d.keysjust doesn't make sense, and neither doesd[list_a] =. I suggest you edit to fix those errors.