Home > Mobile >  How to make a list of the values of dictionary present in a list?
How to make a list of the values of dictionary present in a list?

Time:02-19

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

CodePudding user response:

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

CodePudding user response:

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)
  • Related