list - Need help to find with dictionary objects in python -
i have dictionary object say:
d = { '25478329': ['17647430', '376088951', '32416061', '43096205'], '43096205': '17647430', '376088951': ['17647430', '25478329', '32416061'], '32416061': ['17647430', '25478329'] }
i need finding key maximum numbers of data. data here mean string values inside list.
your question clearer, if want suggested in comments, work:
def keyfunc(item): key, value = item if isinstance(key, str): return 1 else: return len(key) max(d.items(), key=keyfunc)[0]
here, define key function takes key/value 2-tuple returned dict.items()
, and:
- if value string, returns 1.
- otherwise, returns length of list of strings.
that key function used builtin max()
function return item largest number of strings, , use [0]
key item.
Comments
Post a Comment