Jul 22, 2022 python

Python sort() Function: A Faster and Essential Way to Sort a List in Python

Python sort() Function: A Faster and Essential Way to Sort a List in Python :Iterating through a list is one way to sort a list in Python. You can choose how to print the data based on how you want to use it. However, you either use a nested loop with an if statement or the Python list sort() method to achieve this. But one of the ideal ways to do this is using the Python sort() function.

Read on to find out how to use the nested loop and sort() in Python.

Understanding the Python list sort() function

Sorting entails rearranging a given sequence of elements by an element comparison operator. And this is very much easier if you use the Python list sort() function to sort a list in Python. However, there are two different kinds of sorting functions available in Python.

  • Sorted (): The sorted() method sorts a list, set, and dictionary by following the given pattern, set, and dictionary (which is not a sequence) either in ascending order or in descending order (does Unicode comparison for string char by char). The original sequence is unaffected by this technique.
  • sort(): This is remarkably similar to sorting(), but it modifies the original sequence and doesn’t return anything. Additionally, only lists may be utilized using the list sort() method.

Examples of how to sort a list in Python in ascending order using the Python sort() function:

Using sorted()

B= (1, 11, 2,4,1,5,73,8,9,0)
x = sorted(B)
print(x)


B= (1, 11, 2,4,1,5,73,8,9,0)
x = sorted(B, reverse=True)
print(x)


Ba = ("y", "a", "m", "a", "i", "w", "o", "k")
x = sorted(Ba)
print(x)


 Ba = ("y", "a", "m", "a", "i", "w", "o", "k")
x = sorted(Ba, reverse=True)
print(x)

#output 

[0, 1, 1, 2, 4, 5, 8, 9, 11, 73]

[73, 11, 9, 8, 5, 4, 2, 1, 1, 0]

[‘a’, ‘a’, ‘i’, ‘k’, ‘m’, ‘o’, ‘w’, ‘y’]

[‘y’, ‘w’, ‘o’, ‘m’, ‘k’, ‘i’, ‘a’, ‘a’]

Using sort ()

# sorting a Floating point numbers 

dec_num = [2.01, 2.00, 3.67, 3.28, 1.68] 

   
# Sorting list of Floating point numbers 
dec_num.sort()    
print(dec_num) 

   
# List of strings 

words = ["Geeks", "For", "Geeks"] 

   
# Sorting list of strings 
words. sort()   

print(words)

# Integers 

Int_num = [1, 3, 4, 2,7,8,5,9] 

   
# Sorting list of Integers 
Int_num.sort()   

print(Int_num)

#output

[1.28, 1.8, 3.0, 5.7, 6.8, 7.0, 7.8, 9.2]

[‘enumerate’, ‘loop’, ‘rat’, ‘sort’, ‘while’]

[1, 2, 3, 4, 5, 7, 8, 9]

How to Sort a List Without Using the Python sort() Function 

This may involve writing several lines of code, but it gives you control over the sorting process. Additionally, it completely teaches you how to sort a list in Python.

Nitro_list = [7, 8, 6, 4, 2, 3, -1, -2, 0, 1, 5, 10]

for i in range(len(Nitro_list)):
    for j in range(i + 1, len(Nitro_list)):

        if Nitro_list[i] > Nitro_list[j]:
            Nitro_list[i], Nitro_list[j] = Nitro_list[j], Nitro_list[i]

print("this list is arranged in ascending order", Nitro_list)

#output

This list is arranged in ascending order [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10]

Example showing how to sort string alphabetically in Python 

In order to sort a string in Python alphabetically without using the sort function, it might be necessary to convert the string to a list. This is because the string is an immutable data type in Python, whereas the list is mutable.

string_input=input(" please enter the string you want to sort:") 

list_to_string=[] 

len_of_string=len(string_input)

for k in string_input: 
    list_to_string.append(k) 

print(list_to_string)

x=0

for x in range(0,len_of_string):

   print(list_to_string[x])

#output

please enter the string you want to sort: Generated

[‘G’, ‘e’, ‘n’, ‘e’, ‘r’, ‘a’, ‘t’, ‘e’, ‘d’]

G

e

n

e

r

a

t

e

d

You can choose to sort this list in ascending or descending order. But you cannot sort a list in Python which contains string and numeric values.

Index