user19657580
user19657580

Reputation: 39

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

Upvotes: 0

Views: 34

Answers (1)

Erastus Nzula
Erastus Nzula

Reputation: 126

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

Upvotes: 1

Related Questions