Python | Alternate Sort in String list
Sometimes, while working with Python list, we can have a problem in which we need to perform sorting only of alternatively in list. This kind of application can come many times. Let’s discuss certain way in which this task can be performed.
Method : Using
join() + enumerate() + generator expression +
This task can be achieved by using combination of functionalities above. In this, we perform the sort using just %2 elements in String list. The extension of this operation to entire list is performed by generator expression.
The original list : ['cdab', 'gfeh', 'kjil'] The String list after alternate sorting : ['abcd', 'hgfe', 'ijkl']