5.9k views
0 votes
Write a program that extracts the last three items in the list sports and assigns it to the variable last. Make sure to write your code so that it works no matter how many items are in the list.

1 Answer

3 votes

Answer:

Following is the program in python programming language

sports = [14, 25, 32, 63, 79, 38, -10] #list sports

print("The original list is : " + str(sports)) #display original list

last = sports[-3:] #extracts the last 3 items from the list

print("The last 3 elements of list is : " + str(last)) #display the last 3 item

Output:

The original list is : [14, 25, 32, 63, 79, 38, -10]

The last 3 elements of list is: [79, 38, -10]

Step-by-step explanation:

Following are the description of program

  • Declared and initialized the items in the list "sports".
  • After that display the list "sports" by using str function in the python language
  • sports[-3: this will extract the last 3 three items in the list and store the result in the last variable .
  • Finally display the last variable it will print the last 3 element in the list
User Joachim Wagner
by
4.9k points