Skip to content
Advertisement

Sorting lists within a list in Python

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]]

Advertisement

Answer

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]]
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement