# Python – Get last N elements from given list

## Python | Get last N elements from given list

Accessing elements in a list has many types and variations. These are an essential part of Python programming and one must have the knowledge to perform the same. This article discusses ways to fetch the last N elements of list. Let’s discuss certain solution to perform this task.

Method #1 : Using list slicing
This problem can be performed in 1 line rather than using a loop using the list slicing functionality provided by Python. Minus operator specifies slicing to be done from rear end.

 `# Python3 code to demonstrate` `# Get last N elements from list` `# using list slicing` `# initializing list` `test_list ``=` `[``4``, ``5``, ``2``, ``6``, ``7``, ``8``, ``10``]` `# printing original list` `print``(``"The original list : "` `+` `str``(test_list))` `# initializing N` `N ``=` `5` `# using list slicing` `# Get last N elements from list` `res ``=` `test_list[``-``N:]` `# print result` `print``(``"The last N elements of list are : "` `+` `str``(res))`

Output :

```The original list : [4, 5, 2, 6, 7, 8, 10]
The last N elements of list are : [2, 6, 7, 8, 10]```

Method #2 : Using islice() + reversed()
The inbuilt functions can also be used to perform this particular task. The islice function can be used to get the sliced list and reversed function is used to get the elements from rear end.

 `# Python3 code to demonstrate` `# Get last N elements from list` `# using islice() + reversed()` `from` `itertools ``import` `islice` `# initializing list` `test_list ``=` `[``4``, ``5``, ``2``, ``6``, ``7``, ``8``, ``10``]` `# printing original list` `print``(``"The original list : "` `+` `str``(test_list))` `# initializing N` `N ``=` `5` `# using islice() + reversed()` `# Get last N elements from list` `res ``=` `list``(islice(``reversed``(test_list), ``0``, N))` `res.reverse()` `# print result` `print``(``"The last N elements of list are : "` `+` `str``(res))`

Output :

```The original list : [4, 5, 2, 6, 7, 8, 10]
The last N elements of list are : [2, 6, 7, 8, 10]```

Last Updated on October 29, 2021 by admin

## Python List count() methodPython List count() method

Python List count() method Python List count() is an inbuilt function in Python that returns the count of how many times a given object occurs in a List. The count() function is used to count elements on a list as well as a

## Types of inheritance PythonTypes of inheritance Python

Types of inheritance Python Inheritance is defined as the capability of one class to derive or inherit the properties from some other class and use it whenever needed. Inheritance provides the following properties: It represents real-world relationships well. It provides

## Remove all duplicates from a given string in PythonRemove all duplicates from a given string in Python

Remove all duplicates from a given string in Python We are given a string and we need to remove all duplicates from it? What will be the output if the order of character matters? Examples: Input : geeksforgeeks Output :

## Python Membership and Identity OperatorsPython Membership and Identity Operators

Python Membership and Identity Operators Membership Operators Membership operators are operators used to validate the membership of a value. It tests for membership in a sequence, such as strings, lists, or tuples. in operator: The ‘in’ operator is used to check

## Fastest way to Convert Integers to Strings in Pandas DataFrameFastest way to Convert Integers to Strings in Pandas DataFrame

Fastest way to Convert Integers to Strings in Pandas DataFrame   Pandas – An open-source library which is used by any programmer. It is a useful library that is used for analyzing the data and for manipulating the data. It

## Generate all permutation of a set in PythonGenerate all permutation of a set in Python

Generate all permutation of a set in Python Permutation is an arrangement of objects in a specific order. Order of arrangement of object is very important. The number of permutations on a set of n elements is given by  n!.  For example,

## Count number of lines in a text file in PythonCount number of lines in a text file in Python

Count number of lines in a text file in Python Counting the number of characters is important because almost all the text boxes that rely on user input have a certain limit on the number of characters that can be

## Python PIL | Image.show() methodPython PIL | Image.show() method

Python PIL | Image.show() method PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides