Filter Python Dictionary

This article uses python 3.4.1

>>> lst
[{‘author’: {‘value’: ‘http://dbpedia.org/resource/Graham_Roland', ‘type’: ‘literal’}, ‘work’: {‘value’: ‘http://dbpedia.org/resource/Five-Twenty-Ten', ‘type’: ‘uri’}}, {‘author’: {‘value’: ‘http://dbpedia.org/resource/Seiichi_Hayashi\nhttp://dbpedia.org/resource/Seiichi_Hayashi', ‘type’: ‘literal’}, ‘work’: {‘value’: ‘http://dbpedia.org/resource/Red_Colored_Elegy', ‘type’: ‘uri’}}, {‘author’: {‘value’: ‘http://dbpedia.org/resource/John_Riggi', ‘type’: ‘literal’}, ‘work’: {‘value’: ‘http://dbpedia.org/resource/Alexis_Goodlooking_and_the_Case_of_the_Missing_Whisky', ‘type’: ‘uri’}}]

I will filter the list ‘lst’ to ‘lst_result’.

>>> lst_result
[{‘author’: ‘http://dbpedia.org/resource/Graham_Roland', ‘work’: ‘http://dbpedia.org/resource/Five-Twenty-Ten'}, {‘author’: ‘http://dbpedia.org/resource/Seiichi_Hayashi\nhttp://dbpedia.org/resource/Seiichi_Hayashi', ‘work’: ‘http://dbpedia.org/resource/Red_Colored_Elegy'}, {‘author’: ‘http://dbpedia.org/resource/John_Riggi', ‘work’: ‘http://dbpedia.org/resource/Alexis_Goodlooking_and_the_Case_of_the_Missing_Whisky'}]

To simplify the problem, make a temporary variable ‘mydict’.

>>> mydict = lst[0]
{‘author’: {‘value’: ‘http://dbpedia.org/resource/Seiichi_Hayashi\nhttp://dbpedia.org/resource/Seiichi_Hayashi', ‘type’: ‘literal’}, ‘work’: {‘value’: ‘http://dbpedia.org/resource/Red_Colored_Elegy', ‘type’: ‘uri’}}
I used ‘generator’ in Python to filter keys.
>>> mydict = {k: v[‘value’] for k, v in mydict.items()}
{‘author’: ‘http://dbpedia.org/resource/Seiichi_Hayashi\nhttp://dbpedia.org/resource/Seiichi_Hayashi', ‘work’: ‘http://dbpedia.org/resource/Red_Colored_Elegy'}

mydict is an element of list ‘lst’. So it should be iterated.

>>> lst = 
[ {k: v[‘value’] for k, v in mydict.items()} for mydict in lst ]

‘lst’ has filtered.

>>> lst
[{‘author’: ‘http://dbpedia.org/resource/Graham_Roland', ‘work’: ‘http://dbpedia.org/resource/Five-Twenty-Ten'}, {‘author’: ‘http://dbpedia.org/resource/Seiichi_Hayashi\nhttp://dbpedia.org/resource/Seiichi_Hayashi', ‘work’: ‘http://dbpedia.org/resource/Red_Colored_Elegy'}, {‘author’: ‘http://dbpedia.org/resource/John_Riggi', ‘work’: ‘http://dbpedia.org/resource/Alexis_Goodlooking_and_the_Case_of_the_Missing_Whisky'}]
Show your support

Clapping shows how much you appreciated Miae Kim’s story.