Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/nhk.py (Python)

Code before:
            headline = (next((i for i in detail_list if i.get('headline_id') == headline_id), None))
            result = self._extract_episode_info(headline, programme_id)
            result['series'] = series
            result['channel'] = channel
            result['thumbnail'] = result.get('thumbnail') or thumbnail
        else:
            # we are downloading a whole playlist/show
            series_description = meta.get('site_detail')
            # series_url = meta.get('share_url')
            result = {
                '_type': 'playlist',
                'channel': channel,
                'thumbnail': thumbnail,
                'title': series,
                'description': series_description,
                # 'webpage_url': series_url,
                'id': video_id,
                'entries': [],
            }
            for headline in detail_list:
                ep = self._extract_episode_info(headline, programme_id)
                ep['series'] = series
                ep['channel'] = channel
                ep['thumbnail'] = ep.get('thumbnail') or thumbnail
                result['entries'].append(ep)
        return result


Review comment (refactor):
I would simplify this with `traverse_obj` and `playlist_result`

 - `traverse_obj(meta, ('detail_list', ..., {dict}))` will branch out and return all dicts in the list
 - `traverse_obj(meta, ('detail_list', lambda _, v: v['headline_id'] == headline_id))` will filter for dicts in the list where the `'headline_id'` key's value is the value of the `headline_id` variable
 - if a `traverse_obj` path branches, it will always return a list unless `get_all=False`, then only 1 value will be returned

```suggestion
        if headline_id:
            return self._extract_episode_info(
                traverse_obj(meta, (
                    'detail_list', lambda _, v: v['headline_id'] == headline_id), get_all=False),
                programme_id, series_meta)

        def entries():
            for headline in traverse_obj(meta, ('detail_list', ..., {dict})):
                yield self._extract_episode_info(headline, programme_id, series_meta)

        return self.playlist_result(
            entries(), programme_id, playlist_description=meta.get('site_detail'), **series_meta)
```

Code after:

        def entries():
            for headline in traverse_obj(meta, ('detail_list', ..., {dict})):
                yield self._extract_episode_info(headline, programme_id, series_meta)

        return self.playlist_result(
            entries(), programme_id, playlist_description=meta.get('site_detail'), **series_meta)


class NhkRadioNewsPageIE(InfoExtractor):
    _VALID_URL = r'https?://www\.nhk\.or\.jp/radionews/?(?:$|[?#])'
    _TESTS = [{
        # airs daily, on-the-hour most hours
        'url': 'https://www.nhk.or.jp/radionews/',
        'playlist_mincount': 5,
        'info_dict': {
            'id': 'F261_01',
            'thumbnail': 'https://www.nhk.or.jp/radioondemand/json/F261/img/RADIONEWS_640.jpg',
            'description': 'md5:bf2c5b397e44bc7eb26de98d8f15d79d',
            'channel': 'NHKラジオ第1',
            'title': 'NHKラジオニュース',
        }
    }]

    def _real_extract(self, url):
        return self.url_result('https://www.nhk.or.jp/radio/ondemand/detail.html?p=F261_01', NhkRadiruIE)