Open In App

Python | Alternate Sort in String list

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


Output

The 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_list
Auxiliary Space: O(n)



Similar Reads

Python | Sort alternate numeric and alphabet list
Sometimes, while performing sorting in list, we have a problem in which we need to perform particular type of sorting in which we need to sort in alternate ways in which we have numerics and alphabets sorted in order. Lets discuss certain ways in which this task can be performed. Method #1 : Using isalpha() + isnumeric() + zip_longest() The combina
5 min read
Python Program To Merge A Linked List Into Another Linked List At Alternate Positions
Given two linked lists, insert nodes of the second list into the first list at alternate positions of the first list. For example, if first list is 5->7->17->13->11 and second is 12->10->2->4->6, the first list should become 5->12->7->10->17->2->13->4->11->6 and second list should become empty. The
3 min read
Sort a list in Python without sort Function
Python Lists are a type of data structure that is mutable in nature. This means that we can modify the elements in the list. We can sort a list in Python using the inbuilt list sort() function. But in this article, we will learn how we can sort a list in a particular order without using the list sort() method. Sort a List Without Using Sort Functio
3 min read
Python | List consisting of all the alternate elements
Some of the list operations are quite general and having shorthands without needing to formulate a multiline code is always required. Wanting to construct a list consisting of all the alternate elements of the original list is a problem that one developer faces in day-day applications. Let's discuss certain ways to print all the alternate elements
7 min read
Python | List Initialization with alternate 0s and 1s
The initialization of a list with a single number is a generic problem whose solution has been dealt with many times. But sometimes we require to initialize the list with elements alternatively repeating K no. of times. This has use cases in M.L. or A. I algorithms that require presetting of data in lists. Let's discuss certain ways in which this p
6 min read
Python | Increasing alternate element pattern in list
This particular article solves a very specific issue in which we need to insert every alternate element as the increased size pattern of repeated element to form a pattern. This can have a utility in demonstrative projects. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension + enumerate() List comprehension wi
7 min read
Python | Alternate range slicing in list
List slicing is quite common utility in Python, one can easily slice certain elements from a list, but sometimes, we need to perform that task in non-contiguous manner and slice alternate ranges. Let's discuss how this particular problem can be solved. Method #1 : Using list comprehension List comprehension can be used to perform this particular ta
6 min read
Python | Alternate element summation in list
The problem of getting summation of a list is quite generic and we might some day face the issue of getting the summation of alternate elements and get the list of 2 elements containing summation of alternate elements. Let's discuss certain ways in which this can be performed. Method #1 : Using list comprehension + list slicing + sum() List slicing
5 min read
Python | Alternate Cycling in list
Sometimes, while working with a Python list, we can have a problem in which we need to perform the access/printing of list in different ways. In some variations, there might be a need for printing the list in an alternate cyclic way, i.e printing elements from front and read alternatively. This is a popular problem in-school programming. Let's disc
5 min read
Python | Add element at alternate position in list
Sometimes, while working with Python list, we can have a problem in which we need to add elements in list alternatively i.e at even positions and reorder the list accordingly. This has a potential application in many domains such as day-day programming and competitive programming. Let's discuss certain way in which this problem can be solved. Metho
2 min read
three90RightbarBannerImg