Home > database >  Sorting lists within a list in Python
Sorting lists within a list in Python

Time:07-31

I have a list J consisting of lists. I am trying to sort elements of each list in ascending order. I present the current and expected outputs.

J=[[10, 4, 7], [10, 4],[1,9,8]]
for i in range(0,len(J)):
    J[0].sort()

The current output is

[[4, 7, 10], [10, 4], [1, 9, 8]]

The expected output is

[[4, 7, 10], [4, 10], [1, 8, 9]]

CodePudding user response:

Just remove the range

J=[[10, 4, 7], [10, 4],[1,9,8]]
for i in J:
    i.sort()
print(J)

Output:

[[4, 7, 10], [4, 10], [1, 8, 9]]
  • Related