how to sort list in python without sort function with code examples

Sorting a list in Python is a common task and can be achieved in multiple ways. One of the most commonly used methods is the built-in sort() function. However, sometimes you may need to sort a list without using the sort() function for various reasons.

In this article, we will look at how to sort a list in Python without using the sort() function by using other techniques like using sorted(), using the sorted() and lambda functions, using the sorted() and key functions, and using the sorted() and itemgetter functions.

  1. Using sorted()

The sorted() function is used to return a new sorted list from an iterable. To sort a list without using the sort() function, you can use the sorted() function as follows:

# example list
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
 
# using sorted()
sorted_numbers = sorted(numbers)
 
# sorted list
print("Sorted list:", sorted_numbers)

Output:

Sorted list: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
  1. Using sorted() and lambda function

You can also use the sorted() function along with a lambda function to sort a list in Python. The lambda function can be used to specify the sorting criteria.

# example list
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
 
# using sorted() and lambda
sorted_numbers = sorted(numbers, key=lambda x: x)
 
# sorted list
print("Sorted list:", sorted_numbers)

Output:

Sorted list: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
  1. Using sorted() and key function

Another method to sort a list in Python without using the sort() function is to use the sorted() function along with a key function. The key function can be used to specify the sorting criteria.

# example list
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
 
# using sorted() and key function
def sort_criteria(num):
    return num
 
sorted_numbers = sorted(numbers, key=sort_criteria)
 
# sorted list
print("Sorted list:", sorted_numbers)

Output:

Sorted list: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
  1. Using sorted() and itemgetter function

The itemgetter function from the operator module can also be used along with the sorted() function to sort a list in Python.

# example list
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
 
# using sorted()
Sorting a list is a common operation in programming and is used to rearrange a list of elements in a specific order. Sorting can be performed in ascending or descending order, depending on the requirement.

In addition to sorting a list, it is also possible to sort a list of dictionaries or a list of tuples based on certain criteria. Sorting a list of dictionaries can be done by specifying the key on which the sorting should be performed. Similarly, sorting a list of tuples can be done by specifying the index of the element on which the sorting should be performed.

It is important to note that the `sort()` method sorts the list in place, meaning it modifies the original list, whereas the `sorted()` function returns a new sorted list without modifying the original list.

Finally, when sorting a list in Python, it is important to consider the performance of the sorting algorithm. For small lists, the difference in performance may not be noticeable, but for large lists, it can have a significant impact on the overall performance of the program.

In conclusion, sorting a list in Python can be achieved in multiple ways, and the choice of the sorting method depends on the specific requirements and the size of the list. Whether you use the `sort()` method or the `sorted()` function, it is important to understand the basic concepts of sorting algorithms and the difference between in-place and non-in-place sorting methods.
## Popular questions 
1. What is the difference between the `sort()` method and the `sorted()` function in Python?

The `sort()` method is an in-place sorting method and modifies the original list, whereas the `sorted()` function returns a new sorted list without modifying the original list.

2. How can you sort a list of dictionaries based on a specific key in Python?

To sort a list of dictionaries based on a specific key, you can use the `sorted()` function along with the `key` parameter. For example:

list_of_dicts = [{"age": 25, "name": "John"},
{"age": 30, "name": "Jane"},
{"age": 20, "name": "Bob"}]

sorted_list = sorted(list_of_dicts, key=lambda x: x['age'])

3. How can you sort a list of tuples based on a specific index in Python?

To sort a list of tuples based on a specific index, you can use the `sorted()` function along with the `key` parameter. For example:

list_of_tuples = [(2, "John"), (3, "Jane"), (1, "Bob")]

sorted_list = sorted(list_of_tuples, key=lambda x: x[0])

4. Can you sort a list in descending order using the `sorted()` function in Python?

Yes, you can sort a list in descending order using the `sorted()` function by specifying the `reverse` parameter as `True`. For example:

numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

sorted_list = sorted(numbers, reverse=True)

5. How can you sort a list without using the `sort()` method or the `sorted()` function in Python?

One possible way to sort a list without using the `sort()` method or the `sorted()` function is by using the bubble sort algorithm, which is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. Here's an example implementation of the bubble sort algorithm in Python:

def bubble_sort(list):
for i in range(len(list)):
for j in range(0, len(list)-i-1):
if list[j] > list[j+1]:
list[j], list[j+1] = list[j+1], list[j]
return list

numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

sorted_list = bubble_sort(numbers)

### Tag 
Sorting
Posts created 2498

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top