You are currently viewing Python Get the Last N Elements of a List

How to get the last n elements of a list in Python? Getting the last n elements of a list in Python. You can use the slicing operator [-N:], where N is the number of elements you want to retrieve from the end of the list.

You can get the last n elements of a list in Python using many ways like, list slicing, loop, islice() + reversed(), and, generator functions. In this article, I will explain how to get the last n elements of a list by using all these methods with examples.

Related: You can also get the first N elements of a list in Python.

1. Quick Examples of Getting the Last N Elements of a List

If you are in a hurry, below are some quick examples of how to get the last n elements from the given list.


# Quick examples of getting list last n elements

# Initialize list
mylist = [3, 7, 4, 2, 8, 6, 9, 5]
N = 3

# Example 1: Using list slicing
# Get the last n elements from the list 
last_n_elements = mylist[-N:]

# Example 2: Using list slicing
last_n_elements = mylist[len(mylist)-N:]

# Example 3: Using loop
# Get the last n elements from the list 
x = mylist[::-1]
last_n_elements=[]
i = 0
while(i<N):
    last_n_elements.append(x[i])
    i+=1
last_n_elements.reverse()

# Example 4: Get last N elements from a list
# Using islice() + reversed()
last_n_elements = list(islice(reversed(mylist), 0, N))
last_n_elements.reverse()

# Example 5: Using a generator function
# Get the last n elements from the list 
def get_last_n(lst, N):
    yield lst[len(lst)-N:len(lst)]
last_n_elements = list(get_last_n(mylist, 3))

2. Get the Last N Elements from the List Using List Slicing

You can get the last N elements of a list in Python, you can use slicing with a negative index. For example, you can initialize a list called mylist with 8 integer values, and a variable N with a value of 3 and use negative indexing to slice the list. The expression mylist[-N:] will return a new list that contains the last three elements of the original list.

Here, syntax mylist[-N:], mylist is the name of the list, and N is the number of elements you want to retrieve from the end of the list. The colon (:) indicates that you want to perform list slicing, and the -N tells Python to start counting from the end of the list.


# Initialize list
mylist = [3, 7, 4, 2, 8, 6, 9, 5]
print("Original list: ", mylist)
 
# initializing n
N = 3

# Using list slicing
# Get the last n elements from the list 
last_n_elements = mylist[-N:]
print("The last N elements of a list : ", last_n_elements)

Yields below output.

python list last n elements

In another way, you can get the last N elements from a list using list slicing. For example, you first get the length of the list using the len() function. you then subtract N from the length of the list to get the starting index for our slice. You use this starting index to slice the list from the len(mylist)-N index to the end of the list using mylist[len(mylist)-N:]. This gives us a new list containing the last 3 elements of mylist.


# Using list slicing
last_n_elements = mylist[len(mylist)-N:]
print("The last N elements of a list : ", last_n_elements)

Yields the same output as above.

3. Get the Last N Elements from the List Using loop

You can use for loop to get the last N elements from the list. For example, It first reverses the order of the list using the syntax mylist[::-1]. It then initializes an empty list last_n_elements to store the last N elements of the original list.

It then uses a while loop to iterate over the reversed list x and append the first N elements to the last_n_elements list. It uses a counter variable i to keep track of the number of elements appended. Finally, the reverse method is used to revert the order of the last_n_elements list to match the order of the original list.


# Initialize list
mylist = [3, 7, 4, 2, 8, 6, 9, 5]
print("Original list: ", mylist)
 
# initializing n
N = 3

# Using loop
# Get the last n elements from the list 
x = mylist[::-1]
last_n_elements=[]
i = 0
while(i<N):
    last_n_elements.append(x[i])
    i+=1
last_n_elements.reverse()
print("The last N elements of a list : ", last_n_elements)

Yields the same output as above.

4. Get the Last N Elements from the List Using islice() + reversed()

You can also use the itertools library’s islice() function and the reversed() built-in function to get the last N elements from a list. The islice() function allows you to slice an iterable (such as a list) with the start and end indices, while reversed() function allows you to iterate over the iterable in reverse order.

For example, you first import the islice() function from the itertools library. you then initialize a list called mylist with 8 integer values, and a variable N with a value of 3. You then use the islice() function to slice the reversed list of mylist starting from the index 0 and ending at the index N. You pass the result of reversed(mylist) as the iterable argument to islice() function.

Since the islice() function returns an iterator, you convert it to a list using the list() function. You then reverse the order of the resulting list using the reverse() method.


from itertools import islice

# Initialize list
mylist = [3, 7, 4, 2, 8, 6, 9, 5]
print("Original list: ", mylist)
 
# Initializing n
N = 3

# Get last N elements from list
# Using islice() + reversed()
last_n_elements = list(islice(reversed(mylist), 0, N))
last_n_elements.reverse()
print("The last N elements of a list : ", last_n_elements)

Yields the same output as above.

5. Get the Last N Elements from the List Using a Generator Function

You can use a generator function to get the last N elements from the list. For example, first defines a list mylist with 8 integers. Then it defines a generator function get_last_n(lst, N) that yields the last N elements of a given list lst.

The function get_last_n(lst, N) is called arguments mylist and 3, and the result is converted to a list using the built-in function list(). The resulting list is assigned to last_n_elements.


# Initialize list
mylist = [3, 7, 4, 2, 8, 6, 9, 5]
print("Original list: ", mylist)

# Using a generator function
# Get the last n elements from the list 
def get_last_n(lst, N):
    yield lst[len(lst)-n:len(lst)]

last_n_elements = list(get_last_n(mylist, 3))
print("The last N elements of a list : ", last_n_elements)

# Output:
# Original list:  [3, 7, 4, 2, 8, 6, 9, 5]
# The last N elements of a list :  [[6, 9, 5]]

6. Conclusion

In this article, I have explained how to get the last N elements of a list in Python by using list slicing, loop, islice() + reversed() and, generator functions with examples.

Happy Learning !!