The itertools Python built-in module actually has a groupby function that you could use, but the elements that should be grouped must first be sorted so that the elements that should be grouped are adjacent in the list:
sortkeyfn = key=lambda s:s[1] input = [('11013331', 'KAT'), ('9085267', 'NOT'), ('5238761', 'ETH'), ('5349618', 'ETH'), ('11788544', 'NOT'), ('962142', 'ETH'), ('7795297', 'ETH'), ('7341464', 'ETH'), ('9843236', 'KAT'), ('5594916', 'ETH'), ('1550003', 'ETH')] input.sort(key=sortkeyfn)
Now the input is as follows:
[('5238761', 'ETH'), ('5349618', 'ETH'), ('962142', 'ETH'), ('7795297', 'ETH'), ('7341464', 'ETH'), ('5594916', 'ETH'), ('1550003', 'ETH'), ('11013331', 'KAT'), ('9843236', 'KAT'), ('9085267', 'NOT'), ('11788544', 'NOT')]
groupby returns a sequence of 2 tuples of the form (key, values_iterator) . We want to turn this into a list of dicts, where "type" is the key, and "items" is a list of the 0th element of the tuples returned by the_iterator value. Like this:
from itertools import groupby result = [] for key,valuesiter in groupby(input, key=sortkeyfn): result.append(dict(type=key, items=list(v[0] for v in valuesiter)))
Now result contains your desired dict, as indicated in your question.
Perhaps you can think of it simply by setting out one recorder from this, with a key by type and each value containing a list of values. In your current form, to find the values ββfor a particular type, you have to iterate over the list to find a dict containing the corresponding type βtypeβ, and then get the βitemsβ element from it. If you use a single recorder instead of a list of 1-element dicts, you can find elements for a specific type with one key in the main dict. Using groupby , it will look like this:
result = {} for key,valuesiter in groupby(input, key=sortkeyfn): result[key] = list(v[0] for v in valuesiter)
result now contains this dict (this is similar to intermediate res defaultdict in @KennyTM answer):
{'NOT': ['9085267', '11788544'], 'ETH': ['5238761', '5349618', '962142', '7795297', '7341464', '5594916', '1550003'], 'KAT': ['11013331', '9843236']}
(If you want to reduce this to a single line, you can:
result = dict((key,list(v[0] for v in valuesiter) for key,valuesiter in groupby(input, key=sortkeyfn))
or using the newfangled expression form:
result = {key:list(v[0] for v in valuesiter) for key,valuesiter in groupby(input, key=sortkeyfn)}