Start by preprocessing a_list
into something that can quickly access all values associated with an element from b_list
.
import collections
a_dict = collections.defaultdict(list)
for k,v in a_list:
a_dict[k].append(v)
# a_dict = {'1': [2.0, 3.5, 2.5], '2': [.5, .5, 1]}
# If you want to eliminate duplicate values, such as seen
# with the key '2', use a set instead of a list during the aggregation.
Now it’s just a matter of applying min
to each appropriate value in a_dict
for b in b_list:
print(min(a_dict[b]))
solved Python min value in nested lists [closed]