Jump to content

Sorting first even numbers second odd numbers


Herminio

Recommended Posts

A have a list in Python :

numbers = [ 5, 7, 2, 6, 1, 3,  44,14, 9 ]

how to do a function to sort (ascending or descending) this list, but putting even

numbers first, odd numbers in second place?  ( see result below)

sorted_numbers= [ 2, 6, 14, 44,1, 3, 7, 9]

  • Like 1
Link to comment
Share on other sites

  • 1 year later...
def sortList(myList):
	myList.sort()
    
    evens = []
    odds = []
    for i in myList:
        if (i % 2) == 0:
            evens.append(i)
        else:
            odds.append(i)

    print(evens + odds)
    
numbers = [5, 7, 2, 6, 1, 3, 44, 14, 9]
sortList(numbers)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...