Bootstrap FreeKB - Python (Scripting) - Return the last item in a list
Python (Scripting) - Return the last item in a list

Updated:   |  Python (Scripting) articles

Let's say you have the following list.

#!/usr/bin/python

fruits = ["apple", "banana", "orange", "grapes"]

for fruit in fruits:
  print(fruit)

 

This will print each fruit.

apple
banana
orange
grapes

 

-1: can be used to return the last item in the list.

#!/usr/bin/python

fruits = ["apple", "banana", "orange", "grapes"]

for fruit in fruits:
  print(fruit[-1:])

 

Which should return "grapes" in this example.

grapes

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 61f18e in the box below so that we can be sure you are a human.