A
A
Andrew2021-10-29 16:20:33
Python
Andrew, 2021-10-29 16:20:33

How to create a list of dictionaries from a given list?

There is a list:

['Русский', 'Родной', 'Английский', 'C1 — Продвинутый', 'Французский', 'A2 — Элементарный']

You need to convert it to a list of dictionaries:

[{'Русский': 'Родной'}, {'Английский': 'C1 — Продвинутый'}, {'Французский': 'A2 — Элементарный'}]

How to implement it optimally? It somehow doesn’t work out aesthetically for me, and it doesn’t work correctly:

lang_proficiency = []
for i in range(1, len(t)):
    item = {}
    item[t[i-1]] = t[i]
    lang_proficiency.append(item)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-10-29
@rsytrade

result = [ { arr[i]: arr[i + 1] } for i in range(0, len(arr), 2) ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question