Sayantan's Blog On Python Programming

LIST SORTING OPERATIONS

LIST SORTING OPERATIONS

INTRODUCTIONS

Python lists can be sorted either in forward or backward direction by using below methods:

  • sort() FUNCTION
  • reverse() FUNCTION
  • LIST SLICING OPERATOR ([:])

sort() FUNCTION

sort() FUNCTION helps us to sort the list in ascending order.

# List sorting by using sort() example 1

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list.sort()
print("\nElement of sorted list ->", int_list)

LIST SORTING OPERATIONS : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sorted list -> [100, 150, 200, 400, 500, 600]

# List sorting by using sort() example 2

str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML'] 
print("\nElement of original list ->", str_list)
str_list.sort()
print("\nElement of sorted list ->", str_list)

LIST SORTING OPERATIONS : Output

Element of original list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']

Element of sorted list -> ['C', 'C++', 'Java', 'PHP', 'Python', 'XML']

In the above example we have seen after calling sort() function the list has been sorted in ascending order.

reverse() FUNCTION

reverse() FUNCTION helps us just to reverse the original list.

# List sorting by using reverse() example 1

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list.reverse()
print("\nElement of reversed list ->", int_list)

LIST SORTING OPERATIONS : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of reversed list -> [500, 150, 100, 600, 200, 400]

# List sorting by using reverse() example 2

str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML'] 
print("\nElement of original list ->", str_list)
str_list.reverse()
print("\nElement of reversed list ->", str_list)

LIST SORTING OPERATIONS : Output

Element of original list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']

Element of reversed list -> ['XML', 'PHP', 'C', 'C++', 'Java', 'Python']

In the above example we have seen that the original list is just reversed without sorting.

We can sorted the list in descending order by using both sort() and reverse() functions.

# List sorting by using sort() and reverse() example 3

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list.sort()
print("\nElement of sorted list in ascending order ->", int_list)
int_list.reverse()
print("\nElement of sorted list in descending order ->", int_list)

LIST SORTING OPERATIONS : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sorted list in ascending order -> [100, 150, 200, 400, 500, 600]

Element of sorted list in descending order -> [600, 500, 400, 200, 150, 100]

# List sorting by using sort() and reverse() example 4

str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML'] 
print("\nElement of original list ->", str_list)
str_list.sort()
print("\nElement of sorted list in ascending order ->", str_list)
str_list.reverse()
print("\nElement of sorted list in descending order ->", str_list)

LIST SORTING OPERATIONS : Output

Element of original list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']

Element of sorted list in ascending order -> ['C', 'C++', 'Java', 'PHP', 'Python', 'XML']

Element of sorted list in descending order -> ['XML', 'Python', 'PHP', 'Java', 'C++', 'C']

In the above example, we have seen that the original list has been sorted by both ascending and descending order.

LIST SLICING OPERATOR ([:])

We can reverse any list by using List Slicing operator as well.

# List sorting by using LIST SLICING OPERATOR ([:]) example 1

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)

int_list = int_list[::-1]
print("\nElement of reversed list ->", int_list)

LIST SORTING OPERATIONS : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of reversed list -> [500, 150, 100, 600, 200, 400]

# List sorting by using LIST SLICING OPERATOR ([:]) example 2

str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML'] 
print("\nElement of original list ->", str_list)

str_list = str_list[::-1]
print("\nElement of reversed list ->", str_list)

LIST SORTING OPERATIONS : Output

Element of original list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']

Element of reversed list -> ['XML', 'PHP', 'C', 'C++', 'Java', 'Python']

In the above example we have seen that the list has been reversed by using list slicing operator.

Now we can sort the list in ascending order by using sort() function. Then we can reverse the list by using slicing operator.

# List sorting by using LIST SLICING OPERATOR ([:]) example 3

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)

int_list.sort()
print("\nElement of sorted list in ascending order ->", int_list)

int_list = int_list[::-1]
print("\nElement of reversed list ->", int_list)

LIST SORTING OPERATIONS : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sorted list in ascending order -> [100, 150, 200, 400, 500, 600]

Element of reversed list -> [600, 500, 400, 200, 150, 100]

# List sorting by using LIST SLICING OPERATOR ([:]) example 4

str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML'] 
print("\nElement of original list ->", str_list)

str_list.sort()
print("\nElement of sorted list in ascending order ->", str_list)

str_list = str_list[::-1]
print("\nElement of reversed list ->", str_list)

LIST SORTING OPERATIONS : Output

Element of original list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']

Element of sorted list in ascending order -> ['C', 'C++', 'Java', 'PHP', 'Python', 'XML']

Element of reversed list -> ['XML', 'Python', 'PHP', 'Java', 'C++', 'C']

So in the above example we have seen that list slicing operator also can be used to sort the list in both ascending and descending order.

LIST SORTING OPERATIONS : Output

RELATED TOPICS:

Leave a Comment

Your email address will not be published. Required fields are marked *