Check if an item is the last in a list in Python

avatar

Borislav Hadzhiev

Last updated: Aug 11, 2022

banner

Photo from Unsplash

Check if an item is the last in a list in Python #

To check if an item is the last in a list:

  1. Use the enumerate function to get tuples of the index and the item.
  2. Use a for loop to iterate over the enumerate object.
  3. If the current index is equal to the list's length minus 1, then it's the last item in the list.
main.py
my_list = ['a', 'b', 'c', 'd'] for index, item in enumerate(my_list): if index == len(my_list) - 1: print(item, 'is last in the list ✅') else: print(item, 'is NOT last in the list ❌') print('--------------------------') if 'd' == my_list[-1]: print('d is last in the list ✅') print('--------------------------') result = '_'.join(my_list) print(result) # 👉️ 'a_b_c_d'

check if item is last in list

We used the enumerate() function to get an enumerate object we can iterate over.

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index, and the second is the item.

main.py
my_list = ['a', 'b', 'c', 'd'] # 👇️ [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')] print(list(enumerate(my_list)))

We used a for loop to iterate over the enumerate object and on each iteration, we check if the current index is equal to the last index in the list.

main.py
my_list = ['a', 'b', 'c', 'd'] for index, item in enumerate(my_list): if index == len(my_list) - 1: print(item, 'is last in the list ✅') else: print(item, 'is NOT last in the list ❌')
Python indexes are zero-based, so the first index in a list is 0, and the last index is len(my_list) - 1.

You can also check if the item is NOT the last item in the list with the not equals (!=) operator.

main.py
my_list = ['a', 'b', 'c', 'd'] for index, item in enumerate(my_list): if index != len(my_list) - 1: print(item, 'is NOT last in the list ❌') else: print(item, 'is last in the list ✅')

The if block runs if the item of the current iteration is not the last item in the list. The else block is optional.

Alternatively, you can iterate over a slice of the list that excludes the last element.

main.py
my_list = ['a', 'b', 'c', 'd'] # 👇️ get a slice of the list without the last item for item in my_list[:-1]: print(item, 'is NOT last in the list ❌') print(my_list[-1], 'is last in the list ✅')

The my_list[:-1] syntax returns a slice of the list that excludes the last element.

The syntax for list slicing is my_list[start:stop:step].

The slice in the example starts at index 0 and goes up to, but not including the last item in the list.

You can also directly check if a value is equal to the last item in the list.

main.py
my_list = ['a', 'b', 'c', 'd'] if 'd' == my_list[-1]: print('d is last in the list ✅')

Negative indices can be used to count backwards, e.g. my_list[-1] returns the last item in the list and my_list[-2] returns the second-to-last item.

If you need to join the items in the list with a specific string, but don't want to add the string after the last item, use the str.join() method.

main.py
my_list = ['a', 'b', 'c', 'd'] result_1 = '_'.join(my_list) print(result_1) # 👉️ 'a_b_c_d' result_2 = ' '.join(my_list) print(result_2) # 👉️ 'a b c d'

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your list contains numbers or other types, convert all of the values to string before calling join().

main.py
my_list = ['a', 1, 'b', 2, 'c'] all_strings = list(map(str, my_list)) result_1 = '_'.join(all_strings) print(result_1) # 👉️ 'a_1_b_2_c' result_2 = ' '.join(all_strings) print(result_2) # 👉️ 'a 1 b 2 c'

The string the method is called on is used as the separator between the elements.

If you don't need a separator and just want to join the iterable's elements into a string, call the join() method on an empty string.

main.py
my_list = ['a', 'b', 'c'] result = ''.join(my_list) print(result) # 👉️ 'abc'
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.