python get last element in list

Do you want to know the information about “python get last element in list”. If yes, you’re in the correct tutorial.

python get last element in list

l = [1,2,3,4,5]
last = l[len(l)-1]
# The smart way

list = ["first item", "second item", "third item"]
print(list[len(list) - 1])

# The proper way
print(list[-1])
if x == my_list[-1]:
    # do what you want
some_list = [1, 2, 3]
some_list[-1]
print(some_list)
#Output = 3
some_list = [1,2,3]
some_list[-1] is the shortest and most Pythonic.
#output = 3
list1 = ['a','b','c']
print(list1[-1])

Final Thoughts

I hope this article helps you to know about “python get last element in list”. If you have any queries regarding this tutorial please let us know via the comment section. Share this article with your friends and family via social networks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coder Diksha. Here, I post about programming to help developers.

Share on:

Leave a Comment