Python ProgrammingPython Programming

Python list of dictionaries get value

thedict = [{"key": 1, "Val1": 'val1 from element 1', "Val2": 'val2 from element 1'},
           {"key": 2, "Val1": 'val1 from element 2', "Val2": 'val2 from element 2'},
           {"key": 3, "Val1": 'val1 from element 3', "Val2": 'val2 from element 3'}]

thelist = [thelist['key'] for thelist in thedict if 'key' in thelist]
print(thelist)


thelist = [thelist['Val1'] for thelist in thedict if 'key' in thelist]
print(thelist)

thelist = [thelist['Val2'] for thelist in thedict if 'key' in thelist]
print(thelist)
Output
[1, 2, 3]
['val1 from element 1', 'val1 from element 2', 'val1 from element 3']
['val2 from element 1', 'val2 from element 2', 'val2 from element 3']