I well aware that I can get len(unsorted_list) for the number of sub lists. The data_point can’t be larger than the data_points in the sub list. I’m looking at keeping out errors due to data_point set to large. The variable data_point could be set in other parts of code. Am being overly concerned about possible bugs or any programer using this lambda is responsible for understanding it and staying with in it’s limits? I am aware of this, so it is not a problem at this time.
from operator import itemgetter
unsorted_list = [20, 1, 20, 40, 1, 0, 0, 0, 0, 0, 0, 0],[5, 2, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],[9, 1, 20, 40, 1, 0, 0, 0, 0, 0, 0, 0]
data_point = 0
result = sorted(unsorted_list, key=itemgetter(data_point))
print('result ', result)
OUTPUT:
result [[5, 2, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0], [9, 1, 20, 40, 1, 0, 0, 0, 0, 0, 0, 0], [20, 1, 20, 40, 1, 0, 0, 0, 0, 0, 0, 0]]
I wish there was a good tutorial about lambda functions.
Thanks.