2

I am trying to sot the following 'tupled' list from smallest to largest:

tuple1:

[('Bread', 3.1), ('Cheese', 8.2), ('Milk', 6.1), ('Pasta', 4.5) ...

I like to sort the tuple by integers, in ascending order - I have attempted the following:

>>> sortedTuple = tuple1.sort(key=lambda x: (x[0],int(x[1]))

However, I receive the following output:

None

My expected output would be:

[('Bread', 3.1), ('Pasta', 4.5), ('Milk', 6.1), ('Cheese', 8.2)...
Enigmatic
  • 3,902
  • 6
  • 26
  • 48
  • You can use `sortedTuple = sorted(tuple1, key=lambda x: x[1])`. The lambda only requires you to specify the index that you want to sort on. – roganjosh Dec 09 '16 at 11:14
  • Your tuple1 is a list btw tuples don't have a sort method. – polku Dec 09 '16 at 11:15
  • 1
    You know `tuple1` is actually a list and the reason you got `None` is because `list.sort` sorts a list *in place* and returns `None` – Chris_Rands Dec 09 '16 at 11:30

3 Answers3

3

You can use sorted built-in function.

from operator import itemgetter

In [268]: tuple1
Out[268]: [('Bread', 3.1), ('Cheese', 8.2), ('Milk', 6.1)]

In [270]: res = sorted(tuple1, key=itemgetter(1))

In [271]: res
Out[271]: [('Bread', 3.1), ('Milk', 6.1), ('Cheese', 8.2)]

key=itemgetter(1) means the same thing as key=lambda x:x[1] that is to say the second element of your tuples will be used as keys for sorting. But it's supposed to be faster.

If you want, here is a discussion about using itemgetter(x) instead of [x]: Why should I use operator.itemgetter(x) instead of [x]?

Community
  • 1
  • 1
arthur
  • 2,319
  • 1
  • 17
  • 24
  • @BenF97 You say that you want to sort the tuple by integers, in ascending order, but what output do you expect for inputs like : `[('Milk', 3.4),('Bread', 3.1), ('Cheese', 3.2)]` ? – arthur Dec 09 '16 at 11:57
  • I'd expect the output to be: `[('Bread', 3.1), ('Cheese', 3.2), ('Milk', 3.4)]`. Would that not apply for your answer? – Enigmatic Dec 09 '16 at 14:57
3

Use sorted() like this:

>>> tuples_list = [('Bread', 3.1), ('Cheese', 8.2), ('Milk', 6.1), ('Pasta', 4.5)]
>>> sorted(tuples_list, key=lambda item: item[1])
[('Bread', 3.1), ('Pasta', 4.5), ('Milk', 6.1), ('Cheese', 8.2)]

sorted() takes a second argument, which is key, in this case, we use lambda to sort our list based on the second item (item[1]) of each tuple. By default, the first item (item[0]) would be used.

ettanany
  • 19,038
  • 9
  • 47
  • 63
  • The question says that he wishes to sort based on the integer value, so you should cast the lambda key to int. – lucianopaz Dec 09 '16 at 11:23
  • @lucianopaz did you take a look at the OP's attempt and expected output? – ettanany Dec 09 '16 at 11:24
  • Yes, you get the same expected output because all values in the example differed by more than one. If this were not the case, the outputs could have been diferent. – lucianopaz Dec 09 '16 at 11:29
  • In any case, BenF97 wishes to sort based on the int value and not on the float value. – lucianopaz Dec 09 '16 at 11:31
0

simple, like this. considered,

a=[('Bread', 3.1), ('Cheese', 8.2), ('Milk', 6.1), ('Pasta', 4.5)]

sort_2_value = sorted(a,key=lambda x: (x[1]))

Output:

>>>
[('Bread', 3.1), ('Pasta', 4.5), ('Milk', 6.1), ('Cheese', 8.2)]
>>> 
Siva Shanmugam
  • 662
  • 9
  • 19