by List compression with sorted method and string join method
>>> l1 = ["bba", "yxx", "abc"] >>> [''.join(sorted(i)) for i in l1] ['abb', 'xxy', 'abc'] >>>
by lambda
>>> l1 ['bba', 'yxx', 'abc'] >>> map(lambda x:"".join(sorted(x)),l1) ['abb', 'xxy', 'abc']
For Python beginner
- Iterate every item of list
l1 by for loop. - Use sorted() method to sort item and return list.
- Use
join() method to create string from the list. - Use
append() list method to add sored item to new list l2. - print l2
e.g.
>>> l1 ['bba', 'yxx', 'abc'] >>> l2 = [] >>> for i in l1: ... a = sorted(i) ... b = "".join(a) ... l2.append(b) ... >>> l2 ['abb', 'xxy', 'abc']
["".join(sorted(i)) for i in l1]