0

Possible Duplicate:
Sorting a tuple that contains tuples

hi
I have a list that goes A=[[a,'3'],[g,'1'],[y,2]]
I am looking for a quick way to arrange it according to the numbers (second dimension), so
NewA=[[g,'1'],[y,'2'],[a,'3']]
thanks
ariel

1
  • 1
    The input/output is inconsistant, it is likely supposed to be the string '2' and not the int 2. Commented Dec 14, 2010 at 17:36

1 Answer 1

2
from operator import itemgetter
newA = sorted(a, key=itemgetter(1))
Sign up to request clarification or add additional context in comments.

6 Comments

This returns: [['y', 2], ['g', '1'], ['a', '3']]
@AA: What kind of Python are you using? ideone.com/Du1qg
@a-a, the question's input/output is inconsistent, mixing strings and ints.
Aah my bad, I see where I was wrong.
@Kenny, @AA it returns the results, because 2 is an integer, and 1, 3 are strings. It compares type names str and int.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.