And if there were keys without elements of a list after them ? , I thought.
So I added 'nada:' in front, 'nothing:' in the middle, and ’oops:’ at the end of the list named data.
Then, in these conditions,
the code 1 (in following) with groupy appears to give a completely false result,
the code 2 with defaultdict give a result in which keys 'nada:' , 'nothing:' , and ’oops:’ are absent.
They are also less fast than the simplest solution (code 3: Cameron, user506710)
I had an idea => codes 4 and 5.
Results are OK and executions are faster.
from time import clock
data = ['nada:', # <<<=============
'adjective:',
'nice', 'kind', 'fine',
'noun:',
'benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal',
'nothing:', # <<<=============
'adverb:',
'well', 'nicely', 'fine', 'right', 'okay',
'oops:' # <<<=============
]
#------------------------------------------------------------
from itertools import groupby
te = clock()
dic1 = {}
for i, j in groupby(data, key=lambda x: x.endswith(':')):
if i:
key = next(j).rstrip(':')
continue
dic1[key] = list(j)
print clock()-te,' groupby'
print dic1,'\n'
#------------------------------------------------------------
from collections import defaultdict
te = clock()
dic2 = defaultdict(list)
for s in data:
if s.endswith(":"):
key = s[:-1]
else:
dic2[key].append(s)
print clock()-te,' defaultdict'
print dic2,'\n\n==================='
#=============================================================
te = clock()
dic4 = {}
for x in data:
if x[-1] == ':' :
start = x.rstrip(':')
dic4[start] = []
else:
dic4[start].append(x)
print clock() - te
print dic4,'\n'
#------------------------------------------------------------
te = clock()
dic3 = {}
der = len(data)
for i,y in enumerate(data[::-1]):
if y[-1]==':':
dic3[y[0:-1]] = data[len(data)-i:der]
der = len(data)-i-1
print clock()-te
print dic3,'\n'
#------------------------------------------------------------
te = clock()
dic5 = {}
der = len(data)
for i in xrange(der-1,-1,-1):
if data[i][-1]==':':
dic5[data[i][0:-1]] = data[i+1:der]
der = i
print clock() - te
print dic5
print '\ndic3==dic4==dic5 is',dic3==dic4==dic5
{'adjective': ['nice', 'kind', 'fine'], 'noun': ['benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal'], 'adverb': 'well', 'nicely', 'fine', 'right', 'okay']}{key1: val1, val2, val3}in Python.