2

I know that Python has built-in list functions but I'm curious as to how one would write a function to sum a list and a function to reverse a list. I was able to figure out how to write some other list functions (sort, count, index, etc.) but not these, and I imagine some other languages don't have these built-in functions.

Could someone show me some Python code for those 2 functions, not using any other built-in functions?

3
  • 4
    there would be nothing wrong if it were a homework. It just means that answers should be more explanatory and less solution-oriented. What have you tried? Where do you stuck? What specific problem do you have?
    – jfs
    Nov 8, 2011 at 1:33
  • Still, since it sounds exactly like "do my homework for me", I'd rather help someone with their homework, if they said, "yes, this is homework" but also had put some thought into trying to make a sum of list types.
    – Warren P
    Nov 8, 2011 at 1:47
  • Warren, I dont't lie about those things, I'm old enoguh to have learned the value of doing my own work, I simply expect that when I move on to other languages (I am a math major, so I will not be taking any classes beyond this intro w/ python). That there be more of a learning curve, so I'd like to see as much code that I can understand now. I understand why you would be suspicious though Nov 8, 2011 at 2:00

2 Answers 2

1

For summing a list, you can do:

sum([1, 2, 3, 4])

And for reversing a list, this will return a new, reversed list by using Python's slices:

[1, 2, 3, 4][::-1]

Now if you don't want to use built-in functions:

def sum(lst):
    s = 0
    for e in lst:
        s += e
    return s

def reverse(lst):
    l = []
    for e in lst:
        l = [e] + l
    return l
5
  • 1
    Isn't the point of the question to accomplish these without using built-in functions?
    – Matt Ball
    Nov 8, 2011 at 1:31
  • Sorry, I rushed my answer. I edited my answer according to what the OP expects, care to reconsider the downvote? Nov 8, 2011 at 1:36
  • 3
    your reverse() is O(N**2) and O(N) in space. It can be done in O(N) steps and in constant space (inplace). You could add the code to your answer if you like it.
    – jfs
    Nov 8, 2011 at 1:46
  • I haven't followed J.F's link, but I would definitely do it more like for index in range(len(list) / 2): lst[index], lst[-index-1] = lst[-index-1], lst[index] if you're doing it in place, or for index in range(len(lst) - 1): l.append(lst[-index-1]) if you're not. (May be off-by-one errors in that code, didn't check)
    – agf
    Nov 8, 2011 at 2:41
  • @agf: my code is the same as your first snippet except I use // (the example runs on Python3) and n-1-i indexes instead of -index-1. The second snippet is wrong, you could write it as: L = [lst[i] for i in xrange(len(lst)-1, -1, -1)]
    – jfs
    Nov 13, 2011 at 7:55
1

Sum a list

Straight from the Python manual:

>>> def sum(seq):
...     def add(x,y): return x+y
...     return reduce(add, seq, 0)
>>> sum(range(1, 11))
55
>>> sum([])
0

This could be done as a one-liner (...ish) using lambda (Python's anonymous function syntax):

def sum(seq):
    return reduce(lambda x, y: x + y, seq, 0)

Don't want to use reduce?

def sum(seq):
    total = 0
    for s in seq:
        total += s
    
    return total
1
  • Thanks Matt, i didn't now the manual was so useful, I'm such a noob Nov 8, 2011 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.