Python Practice › Dictionary Programs › Program 44
List of dicts to keys and values
Write a Python program to convert a list of dictionaries into a list of its keys and a list of its values.
Input
[{"sqa": 123, "tools": 456}]Output
[['sqa', 'tools'], [123, 456]]
Approach
- Collect keys and values separately.
Solution
data = [{"sqa": 123, "tools": 456}]keys = [k for d in data for k in d]values = [v for d in data for v in d.values()]print([keys, values])Output
[['sqa', 'tools'], [123, 456]]
Learn the concept
New to this topic? Read the lesson, then come back.