1
>>> a = ['7','30','41','14','39','42']
>>> a.sort()
>>> a
['14', '30', '39', '41', '42', '7']

was not it supposed to print the following??

>>> a
['7', '14', '30', '39', '41', '42']
raiyan22
  • 1,043
  • 10
  • 20
  • 3
    They're sorted as a string rather than as numbers; removing the quotes should cause them to be sorted as you'd expect; i.e. `a = [7,30,41,14,39,42]` – JohnLBevan Apr 29 '18 at 22:38
  • 3
    You're sorting _strings_, not numbers. They sort by alphabetical order. If you want to sort by numerical value, you can either use a list of numbers in the first place, or use a sort key, like `a.sort(key=int)`. – abarnert Apr 29 '18 at 22:38
  • 1
    If you need more information, the duplicate questions have great answers, and the [Sorting HOWTO](https://docs.python.org/3/howto/sorting.html) in the docs has further details. – abarnert Apr 29 '18 at 22:39
  • 1
    If you want to sort numbers, you have to cast string to integers. a.sort(key=int) – Vadim Kharitonov Apr 29 '18 at 22:43

0 Answers0