+ 1

How to print even number at even index and odd number at odd index in list using python.

21st Jun 2019, 5:48 PM
anuradha mall
9 Respuestas
+ 1
Could you explain your question better? We can't really help unless we know what you want to achieve
21st Jun 2019, 5:54 PM
Airree
Airree - avatar
+ 1
Output is like this....[2,1,4,3,6,5....…] i.e, 2 at 0 index 1 at 1 index 4 at 2 index 3 at 3 index... ....and so on
21st Jun 2019, 5:57 PM
anuradha mall
+ 1
l = [2,1,4,3,6,5,8] print([l[i]for i in range(len(l)) if(l[i]+l.index(l[i],i,i+1))%2==0]) change numbers to test
21st Jun 2019, 6:27 PM
Choe
Choe - avatar
+ 1
If we change the list output is not correct please check it once again
21st Jun 2019, 6:32 PM
anuradha mall
+ 1
nope it works. This one is the same but deconstructed and labelled. l = [2,1,4,3,6,5,8] for i in range(len(l)): if (l[i]+l.index(l[i],i,i+1))%2==0: print(l[i],"at",l.index(l[i],i,i+1),"index")
21st Jun 2019, 6:41 PM
Choe
Choe - avatar
+ 1
Plzz check it, by changing the list
21st Jun 2019, 6:46 PM
anuradha mall
+ 1
anuradha mall What outputs and inputs are you expecting, please give us more detail so we can help you better
21st Jun 2019, 6:56 PM
Mo Hani
Mo Hani - avatar
+ 1
[2,1,4,3,6,5,8,7,10,9,12,11,14,13,1615]
21st Jun 2019, 6:59 PM
anuradha mall
0
falses were ignored previously. In here, l = [5,1,4,3,7,5,8] for i in range(len(l)): if (l[i]+l.index(l[i],i,i+1))%2==0: print(l[i],"at",l.index(l[i],i,i+1),"index") else: print(l[i],"and",l.index(l[i],i,i+1),"index neither both even or odd") 5 and 0 index neither both even or odd 1 at 1 index 4 at 2 index 3 at 3 index 7 and 4 index neither both even or odd 5 at 5 index 8 at 6 index
21st Jun 2019, 6:55 PM
Choe
Choe - avatar