Herminio Posted June 9, 2020 Share Posted June 9, 2020 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] 1 Link to comment Share on other sites More sharing options...
lakshitha Posted June 12, 2021 Share Posted June 12, 2021 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now