Filter list of dictionaries

前端 未结 5 564
既然无缘
既然无缘 2021-01-13 08:40

This is my example:

dictlist = [{\'first\': \'James\', \'last\': \'Joule\'}, 
            {\'first\': \'James\',\'last\': \'Watt\'},
            {\'first\':          


        
5条回答
  •  广开言路
    2021-01-13 08:53

    This is very simple solution with list comprehension:

    >>> dictlist = [{'first': 'James', 'last': 'Joule'}, {'first': 'James','last': 'Watt'},{'first': 'Christian','last': 'Doppler'}]
    >>> [x['last'] for x in dictlist if x['first'] == 'Christian']
    ['Doppler']
    

提交回复
热议问题