I have a list of lists in the form:
testdata = [['9034968', 'ETH'], ['14160113', 'ETH'], ['9034968', 'ETH'],
['11111', 'NOT'], ['9555269', 'NOT'], ['15724032', 'ETH'],
['15481740', 'ETH'], ['15481757', 'ETH'], ['15481724', 'ETH'],
['10307528', 'ETH'], ['15481757', 'ETH'], ['15481724', 'ETH'],
['15481740', 'ETH'], ['15379365', 'ETH'], ['11111', 'NOT'],
['9555269', 'NOT'], ['15379365', 'ETH']]
I would like to have a final result which groups the unique name with their values. So in the finall list (or dictinary, or any iterable) there are only two names (ETH and NOT) with a list as the second item of all the other values, e.g:
In [252]: unique_names
Out[252]:
{'ETH': ['9034968',
'14160113',
'9034968',
'15724032',
'15481740',
'15481757',
'15481724',
'10307528',
'15481757',
'15481724',
'15481740',
'15379365',
'15379365'],
'NOT': ['11111', '9555269', '11111', '9555269']}
To achieve this I used a dictionary and the following steps:
unique_names = []
for (x,y) in testdata:
if y not in unique_names:
unique_names.append(y)
# now unique_names is ['ETH', 'NOT']
unique_names = {name:list() for name in unique_names}
for (x,y) in testdata: unique_names[y]=unique_names[y]+[x]
#so finally I get the result above
My problem is:
test_data
is a result of an SQL query with 1000's of entries. My solution is running quite slow (at least that's how it feels).- Can you do this in a more Pythonic manner?
The example data for this question was take from a similar question about sets and list here: Python: Uniqueness for list of lists. Unfortunately, the OP there wanted a different result, but the structure of data was appropriate enough.