list1=[1,2,3,4,5,6]
for x in range(len(list1)):
print("{}+{}".format(list1[x],list1[x+1]))
print(list1[x]+list1[x+1])
this is giving me 1+2=3, 2+3=5, 3+4=7, 4+5=9, 5+6=11. what I want as an out put is : 1+2=3, 3+4=7, 5+6=11
list1=[1,2,3,4,5,6]
for x in range(len(list1)):
print("{}+{}".format(list1[x],list1[x+1]))
print(list1[x]+list1[x+1])
this is giving me 1+2=3, 2+3=5, 3+4=7, 4+5=9, 5+6=11. what I want as an out put is : 1+2=3, 3+4=7, 5+6=11
What you want to do is go through the list in pairs, so change the step argument of range()
to 2
:
list1=[1,2,3,4,5,6]
for x in range(0, len(list1)-1, 2):
print("{}+{}".format(list1[x],list1[x+1]))
print(list1[x]+list1[x+1])
See the range()
function documentation for more.
list1=[1,2,3,4,5,6]
for x in range(0,len(list1)-1,2):
print("{}+{}={}".format(list1[x],list1[x+1],(list1[x]+list1[x+1])))
OUTPUT
1+2=3
3+4=7
5+6=11