List of dicts to keys and values

EasyDictionary Programs · 44 of 94

Write a Python program to convert a list of dictionaries into a list of its keys and a list of its values.

Advertisement
Input
[{"sqa": 123, "tools": 456}]
Output
[['sqa', 'tools'], [123, 456]]

Approach

  1. Collect keys and values separately.

Solution

Python
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]]
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs