+ 2

How to sort only the odd numbers in an list

Help me with this question So if you have a list like[3,5,4,2,6,9,1,7] Then you should return like [1,3,5,4,2,6,7,9].

24th Sep 2021, 7:03 AM
SIDDHARTH A
SIDDHARTH A - avatar
6 Respuestas
24th Sep 2021, 9:55 AM
Iftekhar Ahmed Pranto
Iftekhar Ahmed Pranto - avatar
+ 1
Yes they will be. X =[3,5,4,2,6,9,1,7] Even=[ i for i in X if i%2==0] Odd =[i for i in X if i%2!=0] Even.sort() Odd.sort() Print(Even+Odd) This is just the example.Might have little errors.But you get the idea. X=[1,3] Y=[2,4] Print(x+y) >>>>> [1,3,2,4]
24th Sep 2021, 8:25 AM
Iftekhar Ahmed Pranto
Iftekhar Ahmed Pranto - avatar
+ 1
I am thinking so you are not getting me . So if you have [1,3,2,6, 7,4,5] I want to get the output as [1,3,2,6,5,4,7].
24th Sep 2021, 8:31 AM
SIDDHARTH A
SIDDHARTH A - avatar
+ 1
No. only the odd numbers are sorted and even remains in the same position.
24th Sep 2021, 8:34 AM
SIDDHARTH A
SIDDHARTH A - avatar
25th Sep 2021, 9:07 PM
🌼Faith🌼
🌼Faith🌼 - avatar
- 1
You can split a list in 2. One containing odd other is Even. Then sort both list and merge them.
24th Sep 2021, 8:15 AM
Iftekhar Ahmed Pranto
Iftekhar Ahmed Pranto - avatar