what is the sum of the list elements as out put of this code:- list = ["one","two", "three","four", "ten"] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is the sum of the list elements as out put of this code:- list = ["one","two", "three","four", "ten"]

About understaning the same action taken by using pop() and list[:-1] at the same time. list = ["one","two", "three","four", "ten"] list.pop() print(list[:-1]) # The answer is 3

7th Jun 2020, 9:57 AM
Azad m. A.
Azad m. A. - avatar
4 Answers
+ 3
list = ["one","two", "three","four", "ten"] list.pop() // it removes ''ten'' print(list[:-1]) // this will fetch values from index 0 upto -1(last index which is excluded). So you will only have ["one","two", "three"] as output.
7th Jun 2020, 10:05 AM
Avinesh
Avinesh - avatar
+ 2
I don't understand your question but list.pop() removes the last element in list And then we are left with ["one","two","three","four"] And list[:-1] means start from 0 element and stop before -1(last element) so we are left with ["one","two","three"] after slicing( [list[:-1] }) is performed
7th Jun 2020, 10:07 AM
Abhay
Abhay - avatar
0
That is list[::-1] is reversing the elements for you but list[:-1] excludes the last item...similar to pop()
7th Jun 2020, 10:56 AM
Azad m. A.
Azad m. A. - avatar
0
Az.A. No, -1 is the index of the last element in a list. Similarly -2 is the second last element in the list and so on.
7th Jun 2020, 11:14 AM
Avinesh
Avinesh - avatar