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

by
Jeremy Canfield |
Updated: September 17 2023
| 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