Python Forum
little exercise - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: little exercise (/thread-5146.html)



little exercise - haye - Sep-20-2017

hi, i got a list:

my_list = [1,4,8,17,18,24]
i want the output to be :

Output:
1 3 4 9 1 6
i would like to print the result of number in index2 - number in index1 (4-1) , number in index3 - number in index2 (8-4), number in index4 - number in index3 (17-8) ... etc
and there's probably a "trap" with how to get the "1" in the output?
any help?


RE: little exercise - buran - Sep-20-2017

what have you tried? :-)


RE: little exercise - haye - Sep-20-2017

im trying things starting with :

for i, number in enumerate(my_list):
like:

    for i, number in enumerate(my_list):
        print i, number - number-1
but its obviously not that, i'm newbie


RE: little exercise - buran - Sep-20-2017

OK, that is simple but nevertheless working approach.
Do you know how to access list elements by index? (look here)
Then, try to describe in plain language how you will approach  "the trap" :-) - i.e. to print the first element of the output.



RE: little exercise - haye - Sep-21-2017

still didnt find,
i only know it has to start with:
for i, number in enumerate(my_list): (exo starts with that, so i must use it)

sorry for double post.
I just tried :
for i, number in enumerate(my_list):
       print my_list[i] - my_list[i-1]]
my output is :

Output:
-44 1 2 2 15 24
but i want it to be :

Output:
1 1 2 2 15 24



RE: little exercise - buran - Sep-21-2017

Did you check second part of my post?
(Sep-20-2017, 05:56 PM)buran Wrote: Then, try to describe in plain language how you will approach "the trap" :-) - i.e. to print the first element of the output. Hide/Show



RE: little exercise - haye - Sep-21-2017

fixed, thanks!