Just having trouble with itertools.groupby. Given a list of dictionaries,
my_list= [
"AD01", "AD01AA", "AD01AB", "AD01AC", "AD01AD","AD02", "AD02AA", "AD02AB", "AD02AC"]
from this list, I expected to create a dictionary, where the key is the shortest name and the values are the longest names
example
[
{"Legacy" : "AD01", "rphy" : ["AD01AA", "AD01AB", "AD01AC", "AD01AD"]},
{"Legacy" : "AD02", "rphy" : ["AD02AA", "AD02AB", "AD02AC"]},
]
could you help me please
You can use
itertools.groupby, with somenexts:This is not robust to reordering of the input list.
Also, if there is some "gap" in the input, e.g., if
"AD01"does not have corresponding 'rphy' entries, then it will throw aStopIterationerror as you have found out. In that case you can use a more conventional approach: