It seems like you want to group your record based on the first element in each sub-list. That is what the groupby does. but there is an important preliminary step which is sorting your list based on the first first element in each sub-list. You can do this using the sorted function and use itemgetter as key function.
from operator import itemgetter
from itertools import groupby
result = []
my_list = [['Demo-Site', '10.227.209.139'],
['Demo-Site', '10.227.215.68'],
['Demo-Site', '172.18.74.146'],
['Site', '10.152.114.65'],
['Site', '10.227.211.244'],
['Demo-Site', '10.227.147.98'],
['test', '172.18.74.146']]
Demo groupby
for g, data in groupby(sorted(my_list, key=itemgetter(0)), itemgetter(0)):
print(g)
for elt in data:
print(' ', elt)
yields:
Demo-Site
['Demo-Site', '10.227.209.139']
['Demo-Site', '10.227.215.68']
['Demo-Site', '172.18.74.146']
['Demo-Site', '10.227.147.98']
Site
['Site', '10.152.114.65']
['Site', '10.227.211.244']
test
['test', '172.18.74.146']
As you can see your data is grouped by first element in your sub-list. So all you need now is concatenate (.join) the last elements of the "members" of the same group and then append a list [<given group>, <members string>] to the result list.
>>> for g, data in groupby(sorted(my_list, key=itemgetter(0)), itemgetter(0)):
... result.append([g, ', '.join(elt[1] for elt in data)])
...
>>> result
[['Demo-Site', '10.227.209.139, 10.227.215.68, 172.18.74.146, 10.227.147.98'], ['Site', '10.152.114.65, 10.227.211.244'], ['test', '172.18.74.146']]