'How to make a list of the values of dictionary present in a list?
my_list = [{'Wheels': 1, 'Handle': 1, 'Service': 1},{'Wheels': 2, 'Forks': 1, 'Handle': 1, 'Service': 1}, {'Electronic': 1, 'Sensor': 2, 'Hydraulic': 1,}]
Required Output:
[[1, 1, 1],[2, 1, 1, 1], [1, 2, 1]]
Solution 1:[1]
Simple Pythonic one-liner
my_list = [{'Wheels': 1, 'Handle': 1, 'Service': 1},{'Wheels': 2, 'Forks': 1, 'Handle': 1, 'Service': 1}, {'Electronic': 1, 'Sensor': 2, 'Hydraulic': 1,}]
l = [list(i.values()) for i in my_list]
print(l) # [[1, 1, 1], [2, 1, 1, 1], [1, 2, 1]]
Solution 2:[2]
In python 3.7+:
my_list = [{'Wheels': 1, 'Handle': 1, 'Service': 1},
{'Wheels': 2, 'Forks': 1, 'Handle': 1, 'Service': 1},
{'Electronic': 1, 'Sensor': 2, 'Hydraulic': 1,}]
result = [list(d.values()) for d in my_list]
print(result)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | buran |
