12

How to sort the python list that contains the float values,

list1 = [1, 1.10, 1.11, 1.1, 1.2] 

or

list1 = ['1', '1.10', '1.11', '1.1', '1.2'] 

The expected results is

list_val = ['1', **'1.1', '1.2'**, '1.10', '1.11']

but the returned result in using sort() method returns

[1, 1.1000000000000001, 1.1000000000000001, 1.1100000000000001, 1.2]

or

['1', '1.1', '1.10', '1.11', '1.2'].

But, here 1.2 should come in between 1.1 and 1.10.

mastisa
  • 1,875
  • 3
  • 21
  • 39
sathish
  • 239
  • 1
  • 3
  • 7
  • Hi, just use the sort() methode : >>> list1.sort() – khelili miliana May 19 '16 at 09:10
  • Those are two very different lists. You probably don't really want the second one except when you first get it as input. You can use `floatlist = [float(x) for x in stringlist]` – Andrew Jaffe May 19 '16 at 09:11
  • Thanks for the answer. But this doesn't work. The expected results is list_val = ['1', '1.1', '1.2', '1.10', '1.11'], but the returned result is [1.0, 1.1000000000000001, 1.1100000000000001, 1.1000000000000001, 1.2]. The sort() method returns ['1', '1.1', '1.10', '1.11', '1.2']. Here "1.2" should come in between "1.1" and "1.10". – sathish May 20 '16 at 03:34
  • 2
    Oh wow, this was closed as duplicate before the expected resulting list was added and nobody addressed the actual (poorly asked) question. And with the added expected result every single answer is wrong and the duplicate tag is, too. sathish did not want floats ordered by their numerical value, but more like version numbers ordered. That is a completely different question. And the answer to that question would be here: https://stackoverflow.com/questions/2574080/sorting-a-list-of-dot-separated-numbers-like-software-versions/2574090 – BUFU Feb 09 '21 at 16:42

4 Answers4

17

You can use:

list1 = sorted(list1)

If it is in the second format (as a string) you can use the key parameter to convert it into floats by using:

list1 = sorted(list1, key=float)

The key parameter expects a function that will transform the values before sorting using the transformed values, but keeping the original values

Jan van der Vegt
  • 1,471
  • 12
  • 34
4

You can sort any list in two ways.

  • Using the sorted method :: In this the sorted method will return a sorted list but the actual list remains the same

    x=[1,2,3.1,4.5,2.3]
    y = sorted(x)
    y = sorted(x,key=float) #in case if the values were there as string.
    

    In this case x still remains as [1,2,3.1,4.5,2.3], where as the sorted list i.e [1,2,2.3,3.1,4.5] will be returned and in this case will be assigned to y.

  • Using the sort method call provided for lists :: in this the sort method call will sort the actual list

    x=[1,2,3.1,4.5,2.3]
    x.sort()
    

    In this case x will be sorted , hence if you try to print x it will be like [1,2,2.3,3.1,4.5].

You can use any of these methods according to your requirement.

Hope it helps. Happy Coding :)

Strik3r
  • 1,052
  • 8
  • 15
  • Try with x=[1, 1.10, 1.11, 1.1, 1.2]. You will get wrong output. – sathish May 20 '16 at 03:40
  • @sathish >>> x=[1, 1.10, 1.11, 1.1, 1.2] >>> sorted(x) [1, 1.1, 1.1, 1.11, 1.2] >>> x.sort() >>> x [1, 1.1, 1.1, 1.11, 1.2] that's the output i got !! I think that is the correct order isn't it ?? If not help me im not really good in maths !! – Strik3r May 20 '16 at 04:42
  • 1
    @sathish: `1.10` is the same thing as `1.1`. Did you expect it to be sorted differently from `1.1` somehow? – Martijn Pieters May 20 '16 at 05:49
  • Actually the number sequence will be like 1.1, 1.2, 1.3,...,1.9,1.10,1.11 right? I am expecting the same from my output. – sathish May 20 '16 at 07:01
  • FYI: I had a small workaround of making "1.1" as "1.01" , 1.2 as "1.02" and so on. So here the sorting of [1, 1.10, 1.11, 1.01, 1.02] works fine. This could be a small workaround instead of going for an Algorithm. – sathish May 20 '16 at 07:03
  • @sathish as far as i know, python will treat 1.10 as 1.1 you can try assigning a variable value as 1.10 and try printing it.. it'll be 1.1 not 1.10 !! – Strik3r May 20 '16 at 07:16
2

Just use sorted:

sorted(list1, key=float)

This will convert the element to float before comparing, so it will work for both a list of strings or a list of floats (or ints, for what it's worth).

Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69
1

Use the sort() method.

list1.sort()
print(list1)

The difference between the sort() method and the sorted() method is, that sort() is modifying the object the method is applied on.

In other words, the list is not modified unless it is assigned to a variable when applying the accepted solution, whereas sort() does.

Guenter
  • 465
  • 1
  • 3
  • 14