Sagar
Sagar

Reputation: 59

Python list generate ascending order

  List1 = ['ab_01:2,20,100', 'ab_02:1,300,10', "ab_03:5,400,22","ab_04:8,5050,22"]

I have list how to make it in ascending order. Only check after colon value (in this case 2,1,5,8) to decide ordering and keep all values as it is.

Expected Output:

   List1 = ['ab_02:1,300,10', 'ab_01:2,20,100', "ab_03:5,400,22","ab_04:8,5050,22"]

If it is only numeric then I could have to use sorted(list1, key=int).

The reason why i want this is, I want to iterate from lower value in for loop.

Code I tried and struck in middle to proceed..

List2 = []
for x in List1:
    a = x.split(":")[0].split(",")[0]
    List2.append(a)
sorted(List2, key=int)

Upvotes: 0

Views: 113

Answers (1)

Ivan
Ivan

Reputation: 2675

Try something like this:

sorted(List1, key=lambda x: x.split(':')[1])

Upvotes: 5

Related Questions