Remove last element from list python

In this post, we will see how to remove the last element from a list in python.


Using list.pop()

You can use list.pop() method to remove the last element from the list.

Output:

List Of Countries are: [‘India’, ‘China’, ‘Bhutan’, ‘Nepal’] List Of Countries after removing last element: [‘India’, ‘China’, ‘Bhutan’] Removed country: Nepal

pop will raise index error if the list is empty.


Using del statement

You can use del statement to remove last element.

Output:

List Of Countries are: [‘India’, ‘China’, ‘Bhutan’, ‘ada’] List Of Countries after removing last element: [‘India’, ‘China’, ‘Bhutan’]

Please note that list.pop() returns the remove element, but del statement does not return the removed element.

It raises IndexError if list is empty as it is trying to access an index that is out of range.


Using slicing

We can also use slicing to remove the last element. We can get a sublist of elements except the last element.

Output:

List Of Countries are: [‘India’, ‘China’, ‘Bhutan’, ‘Nepal’] List Of Countries after removing last element: [‘India’, ‘China’, ‘Bhutan’]

Please note that slicing operation will return a new list, so this method is not recommended. You can obviously assign the list to the old one.

It does not throw index error in case the list is empty but creates a copy of the list.

That’s all about how to remove last element from list in python

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 27 December

    Create Array of All NaN Values in Python

    Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63b41da72da34081942602/] [crayon-63b41da72da3a953934283/] We used numpy.empty() […]

  • 27 December

    Call Function from Another Function in Python

    Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63b41da72dc07656760006/] [crayon-63b41da72dc0a015100566/] To call a function, […]

  • 27 December

    [Fixed] NameError Name ‘unicode’ is Not Defined in Python

    Table of ContentsUse str() MethodUse sys.version_info with strUse six.text_type Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-63b41da72dd34777266918/] [crayon-63b41da72dd37981908728/] Unicode is a computing industry standard that ensures that text from most of […]

  • 26 December

    Repeat List N Times in Python

    Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

  • 26 December

    Get First Day of Next Month in Python

    Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

  • 25 December

    Convert String Array to Int Array in Python

    Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.