Python | Alternate Sort in String list Last Updated : 08 May, 2023 Comments Improve Suggest changes Like Article Like Report See More 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 + sorted() 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. Python3 # Python3 code to demonstrate working of # Alternate Sort String list # using join() + enumerate() + generator expression + sorted() # initialize list test_list = ['cdab', 'gfeh', 'kjil'] # printing original list print("The original list : " + str(test_list)) # Alternate Sort String list # using join() + enumerate() + generator expression + sorted() res = ["".join(sorted(j, reverse = i % 2)) for i, j in enumerate(test_list)] # printing result print("The String list after alternate sorting : " + str(res)) Output : The original list : ['cdab', 'gfeh', 'kjil'] The String list after alternate sorting : ['abcd', 'hgfe', 'ijkl'] Time Complexity: O(n*nlogn), where n is the length of the input list. This is because we’re using min() + generator expression which has a time complexity of O(n*nlogn) in the worst case.Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list. Method #2: Using For loop +enumerate()+sorted()+join(). Python3 # Python3 code to demonstrate working of # Alternate Sort String list # using for loop + enumerate() # initialize list test_list = ['cdab', 'gfeh', 'kjil'] # printing original list print("The original list : " + str(test_list)) result = [] for i, word in enumerate(test_list): if i % 2 == 0: result.append(''.join(sorted(word))) else: result.append(''.join(sorted(word, reverse=True))) # printing result print("The String list after alternate sorting : " + str(result)) #this code contributed by tvsk OutputThe original list : ['cdab', 'gfeh', 'kjil'] The String list after alternate sorting : ['abcd', 'hgfe', 'ijkl'] Time Complexity: O(m * nlogn), where m is length of test_list and n is sorting the sub strings of test_listAuxiliary Space: O(n) Create Quiz Comment M manjeet_04 Follow 0 Improve M manjeet_04 Follow 0 Improve Article Tags : Python Python Programs Python list-programs Python-sort Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like