Я предполагаю , что ваш порядок всегда одинаков, то есть в группах по 4. Идея состоит в том, чтобы разбить строки, используя :
, а затем создать пары ключ / значение и использовать вложенные для циклов , .strip()
- избавиться от пробела
lst = ['name: test1', 'email: test1@gmail.com', 'role: test', 'description: test',
'name: test2', 'email: test2@gmail.com', 'role: test2', 'description: test2',
'name: test3', 'email: test3@gmail.com', 'role: test3', 'description: test3']
answer = []
for i in range(0, len(lst), 4):
dic = {}
for j in lst[i:i+4]:
dic[j.split(':')[0]] = j.split(':')[1].strip()
answer.append(dic)
# [{'name': 'test1', 'email': 'test1@gmail.com', 'role': 'test', 'description': 'test'},
# {'name': 'test2', 'email': 'test2@gmail.com', 'role': 'test2', 'description': 'test2'},
# {'name': 'test3', 'email': 'test3@gmail.com', 'role': 'test3', 'description': 'test3'}]
Понимание списка будет выглядеть как
answer = [{j.split(':')[0]:j.split(':')[1].strip() for j in lst[i:i+4]} for i in range(0, len(lst), 4)]