How to sort even and odd numbers in Python

Key takeaways:

  • Purpose of sorting even and odd numbers: This approach categorizes numbers into separate lists of even and odd values, making it easier to process or analyze these subsets individually.

  • Python implementation: The implementation demonstrates Python's ability to handle tasks like list manipulation and sorting efficiently using built-in methods like .append() and .sort().

  • Handling of negative numbers: The solution handles both negative and positive integers correctly, ensuring that the parity of the number determines whether it goes into the even or odd list.

Sorting a list of numbers is a common task in programming, and Python provides powerful tools for achieving this efficiently. When dealing with a list that contains both odd and even numbers, you can sort them into two separate lists based on their parity Parity refers to the property of whether a number is even or odd, determined by its divisibility by 2.. We will explore an approach to achieve this, which will demonstrate how to sort even and odd numbers into distinct lists using Python.

Problem statement

Write a program that takes a list of integers as input, separates the even and odd numbers into two separate lists, sorts each list in ascending order, and then returns the sorted lists.

Solution to sort even and odd numbers

Below is the algorithmic strategy for categorizing a list of numbers in Python into separate sets of odd and even elements:

  1. Create two empty lists, even_list and odd_list, one for even numbers and another for odd numbers.

  2. Iterate through each element in the input list.

  3. Check the parity of the current number (whether it's even or odd).

  4. Append the number to the appropriate list based on its parity.

  5. After iterating through all elements, sort both even and odd lists separately.

  6. The sorted even_list and odd_list lists will now contain the even and odd numbers from the original list, respectively.

canvasAnimation-image
1 of 13

Python code to sort even and odd numbers

Let’s look at the code of the above solution:

def sort_even_odd(input_list):
even_list = []
odd_list = []
for num in input_list:
if num % 2 == 0:
even_list.append(num)
else:
odd_list.append(num)
even_list.sort()
odd_list.sort()
return even_list, odd_list
# Example usage
input_numbers = [9, 2, 7, 4, 5, 8, 3, 6, 1]
sorted_even, sorted_odd = sort_even_odd(input_numbers)
print("Original list:", input_numbers)
print("Sorted even numbers:", sorted_even)
print("Sorted odd numbers:", sorted_odd)

Code explanation

  • Lines 1–14: The function sort_even_odd is created with one parameter, input_list, representing the list of numbers to be sorted. Two empty lists, even_list and odd_list, are initialized to store even and odd numbers separately.

    • Lines 5–9: We use a for loop to separate the even or odd numbers into their respective lists.

    • Lines 11–12: After categorizing odd and even numbers into their respective lists, we can proceed to independently sort each list.

Time complexity

The provided solution has a time complexity of O(nlogn){O(n \log n)} because it sorts both the even and odd lists using a sorting algorithm that operates in this time complexity. The loop that separates even and odd numbers contributes to this complexity.

Space complexity

The space complexity is O(n){O(n)} because the two new lists, even_list and odd_list, each store a portion of the input numbers. As the input size increases, the space required for these lists also increases proportionally.

To further strengthen your coding skills and boost your technical interview preparation, here are some LeetCode problems to practice:

Frequently asked questions

Haven’t found what you were looking for? Contact Us


Can the algorithm handle negative numbers?

Yes, the algorithm works for negative numbers since the modulo operation handles both positive and negative integers.


Does the function modify the original input list?

No, the original input list remains unchanged as the function works with separate lists for even and odd numbers.


Can the function be extended to sort numbers in descending order?

Yes, by passing the argument reverse=True to the sort() method, the lists can be sorted in descending order.


What if all numbers are either even or odd?

If all numbers are even, the odd list will be empty, and vice versa.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved