7 Replies - 681 Views - Last Post: 27 January 2015 - 09:01 AM Rate Topic: -----

#1 The Chief   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 12-November 14

Trying to add elements in a list

Posted 27 January 2015 - 08:20 AM

This is so ridiculous, I have been trying to 30 minutes now to do this and I'm about to smash my computer.

I am being told to add the first 4 elements in a list [1,2,3,4,5,6,7,8,9]

I can do
list1 = [1,2,3,4,5,6,7,8,9]
print(sum(list1))
which sums the whole list but I just cannot figure out the syntax to add the first four elements. The only thing that makes sense to me is this.

print(sum(list1 for x in range(0,4)))



Print the sum of list1 for all elements in the range of 0 to 4. Why is this not working... How hard can it be for god sake

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to add elements in a list

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14490
  • View blog
  • Posts: 58,094
  • Joined: 12-June 08

Re: Trying to add elements in a list

Posted 27 January 2015 - 08:23 AM

Why not use a for loop?
Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11267
  • View blog
  • Posts: 19,309
  • Joined: 19-March 11

Re: Trying to add elements in a list

Posted 27 January 2015 - 08:32 AM

list1[0:4]
Was This Post Helpful? 1
  • +
  • -

#4 The Chief   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 12-November 14

Re: Trying to add elements in a list

Posted 27 January 2015 - 08:38 AM

I'm not sure what you mean, I don't know the syntax.

I can do this

total = 0
list1 = [1,2,3,4,5,6,7,8,9]
for x in list1:
    total+=x
print(total)



but I don't know how to add the first 4 elements only. I don't know the syntax

This doesn't work
total = 0
list1 = [1,2,3,4,5,6,7,8,9]
for x in range(0,4) in list1:
    total+=x
print(total)




Neither does this
total = 0
list1 = [1,2,3,4,5,6,7,8,9]
for x in list1(range(0,4)):
    total+=x
print(total)



This just seems utterly pathetic and just the wrong way of doing it

total = 0
list1 = [1,2,3,4,5,6,7,8,9]
for x in list1:
    total+=x
    if x >= list1[3]:
        break
print(total)



I know exactly what I'm trying to do but I just can't think of the syntax of the code. This is usually why I get stuck on a simple program for 5 hours... because it's like trying to spell a foreign word by guessing 1000 times until eventually you guess correctly. I know what to do, I just don't know how to spell it

View Postjon.kiparsky, on 27 January 2015 - 08:32 AM, said:

list1[0:4]


Thanks. So I wasted 30 minutes and this is how simple it was... the frustration is killing me.

This post has been edited by The Chief: 27 January 2015 - 08:39 AM

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14490
  • View blog
  • Posts: 58,094
  • Joined: 12-June 08

Re: Trying to add elements in a list

Posted 27 January 2015 - 08:45 AM

It's all about knowing the nature of a for loop. You start at 0 and go to some number.. you tell it you want to stop at each i from zero to some number.

Examples galore:
# we know this is a for loop that prints out the range 0 to 4
for i in range(5):
    print (i)

print ("====================")

#we know this is a list of numbers
foo=[1,2,3,4,5,6]

#here we print out a list of numbers for the entire range the list.. aka the length of the list.
#notice we tell the range how far from 0 we want to go.
for i in range(len(foo)):
     print (foo[i])

print ("====================")

#in this isntance we want to go from 0 to 3.
for i in range(3):
     print (foo[i])




Quote

This just seems utterly pathetic and just the wrong way of doing it

Take a break, and take a lap.
Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11267
  • View blog
  • Posts: 19,309
  • Joined: 19-March 11

Re: Trying to add elements in a list

Posted 27 January 2015 - 08:49 AM

View Postmodi123_1, on 27 January 2015 - 10:23 AM, said:

Why not use a for loop?

Because python provides good tools for working with lists, and it's worth learning to use them. One of the main reasons people like python is because it gets the language out of your way and lets you talk about things you care about.

So you could write

s = 0
for i in range (4):
  s += list1[i]



but if you write instead
s = sum (list1[0:4]) 


you get code that talks about more about intent, and less about execution, which I think is the point of writing in a higher-level language.

Quote

Thanks. So I wasted 30 minutes and this is how simple it was... the frustration is killing me.


Nonsense. You learned a lot in those thirty minutes. Trying and failing is how you learn.

Quote

It's all about knowing the nature of a for loop. You start at 0 and go to some number.. you tell it you want to stop at each i from zero to some number.


Well, to be fair, he did give a good example of doing this sum with a for loop - and he correctly saw that this is the wrong way to do it in python. So I think think The Chief deserves some credit here.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14490
  • View blog
  • Posts: 58,094
  • Joined: 12-June 08

Re: Trying to add elements in a list

Posted 27 January 2015 - 08:52 AM

Pffsst.. you think I refresh pages before I post? Nope! My post was based on the pre-edit of #4.
Was This Post Helpful? 0
  • +
  • -

#8 The Chief   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 12-November 14

Re: Trying to add elements in a list

Posted 27 January 2015 - 09:01 AM

Also another reason why I didn't consider a for loop was because I had literally just learned about summing the elements in a list using sum.

"Using sum, add all the elements in the list"

list1 = [1,2,3,4,5,6,7,8,9]
print(sum(list1))



"Now sum the first four elements only"

This is why I was set on using sum, because it seemed like the book wanted me to use this method.

This post has been edited by The Chief: 27 January 2015 - 09:02 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1