0

Say I have a list of dictionaries like this

sampDic = [
{"name": "Tom", "age": 10},
{"name": "Mark", "age": 5},
{"name": "Pam", "age": 7}
]

I want to get the 'name' value for all the dictionaries.

I tried

sampDic[:]['name'] 

but that gives an

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-924869e29ce4> in <module>()
----> 1 sampDic[:]['name']

TypeError: list indices must be integers or slices, not str

error.

So the best I can come up with is

for item in sampDic:
     print(item['name'])

Is there a more efficient and/or pythonic way to do this?

SantoshGupta7
  • 5,607
  • 14
  • 58
  • 116

0 Answers0