1

This is what I have so far... How do I get past having my countone go out of the list and crash the program? Or is there a cleaner way? Also, should I be using range or just having for i in list1? Also is there a way of setting i to have an initial value like in java? Edit: Forgot to mention that I'm practicing for interviews... Probably should avoid using built in functions... Or would this be acceptable?

list1 = [1,2,3,4,5,6,7,8,9,10]
list3 = []
countone = 0

for i in range(0, len(list1)-1):
    firstnum = list1[i+countone]
    secondnum = list1[i+countone+1]
    print firstnum, "+", secondnum
    sumnum = firstnum + secondnum
    list3.append(sumnum)
    countone += 1

print list3
Intrepid Diamond
  • 462
  • 1
  • 6
  • 16

5 Answers5

2

You can use iter and zip:

list1 = [1,2,3,4,5,6,7,8,9,10]

it = iter(list1)

print([a + b for a,b in zip(it,it)])
[3, 7, 11, 15, 19]

iter creates an iterator, once you consume an element from an iterator t is gone and you get the next element:

In [4]: it = iter(list1)

In [5]: next(it)
Out[5]: 1

In [6]: next(it)
Out[6]: 2

In [7]: next(it)
Out[7]: 3

So zip(it,it) is basically calling next(it),next(it) until the iterator is exhausted leaving you with consecutive pairs of elements. It also avoids creating two new lists which will happen if you slice.

If you want a loop, use enumerate with a start index of 1 and append and reset a sm variable each time the ith index is evenly divisible by 2:

sm = 0
out = []
for i, ele in enumerate(list1, 1):
    sm += ele
    if i % 2 == 0:
        out.append(sm)
        sm = 0
print(out)
[3, 7, 11, 15, 19]
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • I have seen this ```iter```/```zip``` *trick* before (is it in the docs?) and I really don't like it - I don't know why, maybe because it seems like a *trick* - it's just a bit too clever. I have to figure out how it works each time I see it. Maybe add an explanation of why/how it works, at least when answering a novice question. – wwii Oct 03 '15 at 22:26
  • @wwii, if you understand how a generator or iterator works then you know how this works, there is something similar in the itertools docs, the grouper recipe. Every time you consume part of an iterator you move to the next element so `zip(it, it)` is the same as calling `next(it),next(it)` until the iterator is exhausted. – Padraic Cunningham Oct 03 '15 at 22:29
  • 1
    Yea I know, and it is useful- just my two bob - [How does ```zip(*[iter(s)]*n)``` work in Python?](http://stackoverflow.com/q/2233204/2823755) – wwii Oct 03 '15 at 22:38
1

If you can use build-in functions you can do:

list(map(sum, zip(a[::2], a[1::2])))

Otherwise change your code to

list1 = [1,2,3,4,5,6,7,8,9,10]
list3 = []

for i in range(0, len(list1)-1, 2):
    firstnum = list1[i]
    secondnum = list1[i+1]
    print(firstnum, "+", secondnum)
    sumnum = firstnum + secondnum
    list3.append(sumnum)

print(list3)

You don't need the variable counton.

sve
  • 4,336
  • 1
  • 19
  • 30
1

You could use slicing to create 2 new lists and zip them together:

list3 = [a+b for a,b in zip(list1[::2], list1[1::2])]
AChampion
  • 29,683
  • 4
  • 59
  • 75
  • Forgot to mention that I'm practicing for interviews... Probably should avoid using built in functions... Or would this be acceptable? – Intrepid Diamond Oct 03 '15 at 22:06
  • I would suggest that builtins would be appropriate for a python question, or else you are severally limited for doing anything. I would also say anything from the standard library should be acceptable unless they explicitly say you can't. – AChampion Oct 03 '15 at 22:09
  • I am interviewing for Microsoft Explore, do you think they'd be okay with it? – Intrepid Diamond Oct 03 '15 at 22:14
  • i would actually think that it would be great to use any builtin functionality during an interview process, but sadly I know from experience that some interviewers like to make really strange prerequisites for the coding. – stian Oct 03 '15 at 22:21
0

Other answers are beautiful and using list comprehensions is a great idea, but I saw your comment, so I propose solution without using builtin functions. I have slightly modified your code, so now it works fine.

for i in range(0,len(list1)-1,2):
    firstnum = list1[i]
    secondnum = list1[i+1]
    print firstnum, "+", secondnum
    sumnum = firstnum + secondnum
    list3.append(sumnum)

You should also note that this code works only for lists with odd number of items. If the list is even, you should add the last element of list1 to list3.

ig-melnyk
  • 2,769
  • 2
  • 25
  • 35
0
from itertools import groupby

[sum(v) for _, v in groupby(list1, lambda x: (x+1) / 2)]
Andrey
  • 59,039
  • 12
  • 119
  • 163