Python – sorting the list in Python without .sorted()

listpython

I have a list a = ['a1', 'b1', 'c1', 'd1', 'a2', 'b2', 'c2', 'd2',]

How can I get list b = ['a1,', 'a2', 'b1', 'b2', 'c1', 'c2', 'd1', 'd2'] without using .sorted()?

Thanks!

Best Answer

There is no .sorted() method for lists, though there is the sorted() function, as S.Mark pointed out (which returns a new sorted list), and a .sort() method (which sorts a list in place and returns None). If you meant to not use the sorted() function, then:

a = ['a1', 'b1', 'c1', 'd1', 'a2', 'b2', 'c2', 'd2',]
a.sort()
b = a

otherwise, maybe you can clarify your question further.