Python Forum
Book exercise in lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Book exercise in lists
#8
(Feb-02-2019, 10:24 PM)sonedap Wrote: I want to make a list which contains other lists in the following pattern:
List=[[1],[1,2],[1,2,3]] etc
the second part is to find sum of each sublist and print a message
like "The first elment of the list is [1] with sum 1"
"The second element of the list is [1,2] with sum 3"
Etc

Thats what this exercise is about

You have a code how to build required list. Now you have to sum sublist elements. Fortunately in Python there is function sum()

Easiest way to get sum is add sum to previous solution and skip converting to list:

result = list()
>>> for i in range(1, 5):
...     result.append(sum(range(1, i+1)))
...
>>> result
[1, 3, 6, 10]
If you think little about this code you probably see, that instead appending to list you can print them out and do that in one loop:

>>> for i in range(1, 5):
...    print(f'The first element of the list is {list(range(1, i+1))} with sum {sum(range(1, i+i))}')
...
The first element of the list is [1]with sum 1
The first element of the list is [1, 2]with sum 3
The first element of the list is [1, 2, 3]with sum 6
The first element of the list is [1, 2, 3, 4]with sum 10
Would you call these two rows of code complicated solution?

I noticed, that there is 'The first' element. It can be easily remedied by adding zip to the code:

>>> for i, v in zip(range(1, 5), ['first', 'second', 'third', 'fourth']):
...     print(f'The {v}  element of the list is {list(range(1, i+1))} with sum {sum(range(1, i+i))}')
...
The first  element of the list is [1] with sum 1
The second  element of the list is [1, 2] with sum 6
The third  element of the list is [1, 2, 3] with sum 15
The fourth  element of the list is [1, 2, 3, 4] with sum 28
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Book exercise in lists - by sonedap - Feb-02-2019, 06:19 PM
RE: Book exercise in lists - by ichabod801 - Feb-02-2019, 06:58 PM
RE: Book exercise in lists - by sonedap - Feb-02-2019, 07:51 PM
RE: Book exercise in lists - by perfringo - Feb-02-2019, 10:15 PM
RE: Book exercise in lists - by ichabod801 - Feb-02-2019, 09:15 PM
RE: Book exercise in lists - by sonedap - Feb-02-2019, 10:08 PM
RE: Book exercise in lists - by sonedap - Feb-02-2019, 10:24 PM
RE: Book exercise in lists - by perfringo - Feb-02-2019, 10:50 PM
RE: Book exercise in lists - by sonedap - Feb-02-2019, 10:58 PM
RE: Book exercise in lists - by perfringo - Feb-02-2019, 11:32 PM
RE: Book exercise in lists - by sonedap - Feb-03-2019, 08:53 AM
RE: Book exercise in lists - by perfringo - Feb-03-2019, 09:11 AM
RE: Book exercise in lists - by sonedap - Feb-03-2019, 10:03 AM
RE: Book exercise in lists - by perfringo - Feb-03-2019, 10:09 AM
RE: Book exercise in lists - by sonedap - Feb-03-2019, 12:29 PM
RE: Book exercise in lists - by perfringo - Feb-03-2019, 12:38 PM
RE: Book exercise in lists - by sonedap - Feb-03-2019, 06:39 PM
RE: Book exercise in lists - by ichabod801 - Feb-03-2019, 08:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  py4e book exercise not working when compiled adriand 11 8,953 Jul-01-2020, 08:42 AM
Last Post: tawhidbd1248
  John Guttag Book - Finger Exercise 4 - need help to make the code better pritesh 12 10,706 May-06-2020, 05:10 PM
Last Post: riteshp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020