+ 2

I want to do separate the even odd from the array list but it occur error...can anyone help me regarding this problem???

n=int(input()) arr=[] even=[] odd=[] for i in range(len(arr)): if i%2==0: even.append(i) else: odd.append(i) print(even,odd)

1st Aug 2021, 2:19 PM
Disha Jain
12 odpowiedzi
+ 4
Disha Jain 1st thing not possible to take list input as you said. 2nd thing if you want to take input like 1,2,3,4 then take as a string input then use split function to get in an array but in this case doesn't make sense of N. You can do like this: str1 = input() arr = str1.split(',') 3rd thing if you want to take N number of input and store in an array then you can do like this: n = int(input ()) arr = [] for i in range(n): arr.append(int(input())
1st Aug 2021, 3:38 PM
A͢J
A͢J - avatar
+ 3
arr is empty and what is n ?
1st Aug 2021, 2:21 PM
Abhay
Abhay - avatar
+ 3
Disha Jain There should be range(n):
1st Aug 2021, 2:22 PM
A͢J
A͢J - avatar
+ 2
Disha Jain What's the use of n and where are elements in arr?
1st Aug 2021, 2:21 PM
A͢J
A͢J - avatar
+ 2
Disha Jain Do this to get arr arr = list(range(n)) print (arr)
1st Aug 2021, 2:34 PM
A͢J
A͢J - avatar
+ 2
Disha Jain There are two ways to solve your problem. Choose whatever you want. ------1st way------ n = int(input()) arr = list(range(n)) even = [] odd = [] for i in arr: if i % 2 == 0: even.append(i) else: odd.append(i) print(even, odd) ----2nd way---- even = [] odd = [] for i in range(n): if i % 2 == 0: even.append(i) else: odd.append(i) print(even, odd)
1st Aug 2021, 2:39 PM
A͢J
A͢J - avatar
+ 2
#Disha Jain # try this : array = [6,7,8,5,4] even=[] odd=[] for i in range(0,len(array)): if i%2==0: even.append(array[i]) else: odd.append(array[i]) print("Even Number :" ,even) print("Odd Numbers :" ,odd)
1st Aug 2021, 2:50 PM
Pariket Thakur
Pariket Thakur - avatar
+ 1
N is the size of array and arr is the array input where I want to insert the array number
1st Aug 2021, 2:28 PM
Disha Jain
+ 1
A͢J OKAY...I got this problem....but if I want to take input like this [6,7,8,5,4] Then this method doesn't apply ...and I want to solve like this
1st Aug 2021, 2:44 PM
Disha Jain
+ 1
Oh I get it you should get elements as input whit space(or something else) in between like 6 7 8 5 4 then: n = input("list") odd = [] even =[] arr = [] for i in n.split(): arr.append(int(i)) for i in arr: if i%2==0: even.append(i) else: odd.append(i)
1st Aug 2021, 3:13 PM
Abs Sh
Abs Sh - avatar
0
Disha Jain do want a input in form of array u can use .split() Here is the examle : arrayy = input() array = arrayy.split() even=[] odd=[] for i in range(0,len(array)): if i%2==0: even.append(array[i]) else: odd.append(array[i]) print("Even Number :" ,even) print("Odd Numbers :" ,odd) Code: https://code.sololearn.com/c0Tet86lQO8J/?ref=app
1st Aug 2021, 3:14 PM
Pariket Thakur
Pariket Thakur - avatar
- 2
U shud do this N = [1,2,3,4,5,6] even = [] odd = [] #without input, it will be same even with input for i in range(n): If I %= 0 : even.append else : odd.append
2nd Aug 2021, 5:06 AM
Jprogsie
Jprogsie - avatar