+ 1
How to print even number at even index and odd number at odd index in list using python.
9 odpowiedzi
+ 1
Could you explain your question better? We can't really help unless we know what you want to achieve
+ 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
+ 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
+ 1
If we change the list output is not correct please check it once again
+ 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")
+ 1
Plzz check it, by changing the list
+ 1
anuradha mall What outputs and inputs are you expecting, please give us more detail so we can help you better
+ 1
[2,1,4,3,6,5,8,7,10,9,12,11,14,13,1615]
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