Is there a way to ordinate numbers in a list?

it seems like you have a list of strings and you want to sort them numerically. to do this you need to tell sort that it needs to sort each element as if it were an int.

my_nums = ['22', '25', '75', '52', '70', '14', '5', '60', '81', '83', '72', '2', '36', '78', '10', '65', '43', '74', '51', '9', '29', '49', '24', '76', '23', '67', '35', '8', '85', '59', '18', '66', '38', '27', '19', '57', '77', '42', '84', '11', '46', '13', '89', '62', '7', '39', '32', '50', '86', '44', '64', '79', '54', '12', '68', '34', '15', '69', '71', '45', '20', '41', '82', '16', '1', '48', '37', '58', '61', '56', '53', '40', '80', '31', '87', '73', '90', '3', '88', '55', '30', '21', '4', '63', '26', '28', '33', '6', '17']
my_nums.sort(key=int)
print(my_nums)

OUTPUT

['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90']

Browse More Popular Posts

Leave a Comment