+ 1

Any one can save this problem. I want output like [ [1,2] [3,5] ] , https://code.sololearn.com/c42C2c3hRp42/?ref=app

i=[] n=input() n=int(n) i.append(n) def oe(l): od=[] e=[] for i in l: if i%2==0: e.append(i) else: od.append(i) total=[od,e] return total print (oe(i)) #Im input like __ 1,2,3,4,5,6,7,8,9, #Output ---- [ [1,2,4,6,8] [3,5,7,9] ] #Odd even numbers

29th Feb 2020, 4:13 PM
Jimmy
Jimmy - avatar
2 odpowiedzi
29th Feb 2020, 4:45 PM
Oma Falk
Oma Falk - avatar
+ 1
I suppose you want to arrange even and odd numbers separately in a nested list. Instead of for loops it uses 2 comprehensions. This can be done also like this: nums = [3,1,4,9,8,5,6,7,2] out = [] out.append([x for x in sorted(nums) if x%2 == 0]) out.append([y for y in sorted(nums) if y%2 != 0]) print(out) # [[2, 4, 6, 8], [1, 3, 5, 7, 9]]
29th Feb 2020, 8:42 PM
Lothar
Lothar - avatar