Python - Sort from Kth index in List
Last Updated :
18 Apr, 2023
Given a list of elements, perform sort from Kth index of List.
Input : test_list = [7, 3, 7, 6, 4, 9], K = 2
Output : [7, 3, 4, 6, 7, 9]
Explanation : List is unsorted till 3 (1st index), From 2nd Index, its sorted.
Input : test_list = [5, 4, 3, 2, 1], K= 3
Output : [5, 4, 3, 1, 2]
Explanation : Only last two elements, 1, 2 are sorted.
Method #1 : Using loop + list slicing
This is one of the ways in which this task can be performed. In this, we extract each element before K in list and then perform list slice using slice symbol and perform sort over the extracted sliced list.
Python3
# Python3 code to demonstrate working of
# Perform sort from Kth index
# Using loop + list slicing
# initializing list
test_list = [8, 4, 7, 3, 2, 14, 6]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 3
# Using loop + list slicing
res = []
# Using loop to extract elements till K
for idx, ele in enumerate(test_list):
if idx < K:
res.append(ele)
# join sorted and unsorted segments
res = res + sorted(test_list[K:])
# printing result
print("Partially sorted list : " + str(res))
OutputThe original list : [8, 4, 7, 3, 2, 14, 6]
Partially sorted list : [8, 4, 7, 2, 3, 6, 14]
Time Complexity: O(n*logn), as sorted function is used.
Auxiliary Space: O(n), where n is the size of list
Method #2 : Using double List slicing
This is yet another way in which we perform this task. In this, we perform the task of list slicing for slicing the unsort part as well and perform concatenation, delivering a one liner alternative to solve this problem.
Python3
# Python3 code to demonstrate working of
# Perform sort from Kth index
# Using double List slicing
# initializing list
test_list = [8, 4, 7, 3, 2, 14, 6]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 3
# Using loop + list slicing
res = []
# Using loop to extract elements till K
# Concatenating unsort and sorted part as one liner
res = test_list[:K] + sorted(test_list[K:])
# printing result
print("Partially sorted list : " + str(res))
OutputThe original list : [8, 4, 7, 3, 2, 14, 6]
Partially sorted list : [8, 4, 7, 2, 3, 6, 14]
Time Complexity: O(n*logn), as sorted function is used.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Similar Reads
Python - Sort Records by Kth Index List Sometimes, while working with Python Records, we can have a problem in which we need to perform Sorting of Records by some element in Tuple, this can again be sometimes, a list and sorting has to performed by Kth index of that list. This is uncommon problem, but can have usecase in domains such as w
4 min read
Sort a list in python Sorting is a fundamental operation in programming, allowing you to arrange data in a specific order. Here is a code snippet to give you an idea about sorting.Python# Initializing a list a = [5, 1, 5, 6] # Sort modifies the given list a.sort() print(a) b = [5, 2, 9, 6] # Sorted does not modify the gi
5 min read
Python - Sort Dictionary List by Key's ith Index value Given List of dictionaries, sort dictionaries on basis of Key's ith index value Input : [{"Gfg" : "Best", "for" : "Geeks"}, {"Gfg" : "Good", "for" : "Me"}, {"Gfg" : "Better", "for" : "All"}], K = "Gfg", i = 1 Output : [{'Gfg': 'Best', 'for': 'Geeks'}, {'Gfg': 'Better', 'for': 'All'}, {'Gfg': 'Good',
7 min read
Python | Sort Flatten list of list The flattening of list of lists has been discussed earlier, but sometimes, in addition to flattening, it is also required to get the string in a sorted manner. Let's discuss certain ways in which this can be done. Method #1 : Using sorted() + list comprehension This idea is similar to flattening a l
7 min read
Python - Sort dictionaries list by Key's Value list index Given list of dictionaries, sort dictionaries on basis of Key's index value. Input : [{"Gfg" : [6, 7, 8], "is" : 9, "best" : 10}, {"Gfg" : [2, 0, 3], "is" : 11, "best" : 19}, {"Gfg" : [4, 6, 9], "is" : 16, "best" : 1}], K = "Gfg", idx = 0 Output : [{'Gfg': [2, 0, 3], 'is': 11, 'best': 19}, {'Gfg': [
14 min read
Python - Sort by Units Digit in List Given a Integer list, sort by unit digits. Input : test_list = [76, 434, 23, 22342] Output : [22342, 23, 434, 76] Explanation : 2 < 3 < 4 < 6, sorted by unit digits. Input : test_list = [76, 4349, 23, 22342] Output : [22342, 23, 76, 4349] Explanation : 2 < 3 < 6 < 9, sorted by unit
7 min read
Sort mixed list in Python Sometimes, while working with Python, we can have a problem in which we need to sort a particular list that has mixed data types. That it contains integers and strings and we need to sort each of them accordingly. Let's discuss certain ways in which this problem can be solved. Method #1: Using sort(
4 min read
Python - Nearest K Sort Given a List of elements, perform sort on basis of its distance from K. Input : test_list = [6, 7, 4, 11, 17, 8, 3], K = 10 Output : [11, 8, 7, 6, 4, 17, 3] Explanation : 11-10 = 1; < 10 - 8 = 2 .. Ordered by increasing difference. Input : test_list = [6, 7, 4, 11], K = 10 Output : [11, 7, 6, 4]
4 min read
Python | Sort dictionary by value list length While working with Python, one might come to a problem in which one needs to perform a sort on dictionary list value length. This can be typically in case of scoring or any type of count algorithm. Let's discuss a method by which this task can be performed. Method 1: Using sorted() + join() + lambda
4 min read
Python - Sort Dictionary key and values List Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the sorting of it, wrt keys, but also can have a variation in which we need to perform a sort on its values list as well. Let's discuss certain way in which this task can be performed. Input : test_d
6 min read