Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Remove Multiple Elements from List in Python
Next article icon

Python - Remove rear element from list

Last Updated : 06 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

A stack data structure is a very well-known data structure, lists in Python usually append the elements to the end of the list. For implementing a stack data structure, it is essential to be able to remove the end element from a list. Let’s discuss the ways to achieve this so that stack data structure can be implemented easily using lists.

Method #1: Using pop(-1) 

This method pops, i.e removes and prints the ith element from the list. This method is mostly used among the other available options to perform this task. This changes the original list. 

Approach:

  1. Initialize a list named test_list with some elements.
  2. Print the original list using the print() function and the str() function to convert the list to a string.
  3. Use the pop(-1) method to remove the last element from the list. The pop() method modifies the list in-place and returns the removed element.
  4. Print the modified list using the print() function and the str() function to convert the list to a string.
Python3
# Python 3 code to demonstrate  # Remove rear element # using pop(-1)  # initializing list  test_list = [1, 4, 3, 6, 7]  # Printing original list print("Original list is : " + str(test_list))  # using pop(-1) to # Remove rear element test_list.pop(-1)  # Printing modified list  print("Modified list is : " + str(test_list)) 
Output : 
Original list is : [1, 4, 3, 6, 7] Modified list is : [1, 4, 3, 6]

Time complexity: O(1) - The pop() method takes constant time to remove the last element from the list.
Auxiliary space: O(1) - No extra space is used in this code.

Method #2: Using del list[-1] This is just the alternate method to perform the rear deletion, this method also performs the removal of list element in place and decreases the size of list by 1. 

Python3
# Python 3 code to demonstrate # Remove rear element # using del list[-1]  # initializing list test_list = [1, 4, 3, 6, 7]  # Printing original list print (& quot         Original list is : & quot         + str(test_list))  # using del list[-1] to # Remove rear element del test_list[-1]  # Printing modified list print (& quot         Modified list is : & quot         + str(test_list)) 
Output : 
Original list is : [1, 4, 3, 6, 7] Modified list is : [1, 4, 3, 6]

Time complexity: O(1)
Auxiliary space: O(1)

Method #3 : Using slicing + len() method

Python3
# Python 3 code to demonstrate # Remove rear element  # initializing list test_list = [1, 4, 3, 6, 7]  # Printing original list print("Original list is : " + str(test_list))  # Remove rear element test_list = test_list[:len(test_list)-1] # Printing modified list print("Modified list is : " + str(test_list)) 

Output
Original list is : [1, 4, 3, 6, 7] Modified list is : [1, 4, 3, 6]

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #4: Using a list comprehension method.

Using a list comprehension to create a new list without the last element.

Python3
# Python 3 code to demonstrate # Remove rear element  # initializing list test_list = [1, 4, 3, 6, 7]  # Printing original list print("Original list is : " + str(test_list))  # Remove rear element using list comprehension test_list = [x for x in test_list if test_list.index(x) != len(test_list) - 1] # Printing modified list print("Modified list is : " + str(test_list)) 

Output
Original list is : [1, 4, 3, 6, 7] Modified list is : [1, 4, 3, 6]

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #4: Using a list comprehension method.

This approach involves using the built-in method list.remove() to remove the last element from the list by specifying the element to be removed. It does not require the use of additional data structures or creation of a new list.

Approach:

  1. Create a list test_list with the values [1, 4, 3, 6, 7].
  2. Print the original list using print("Original list is : " + str(test_list)). This will output "Original list is : [1, 4, 3, 6, 7]".
  3. Use the remove() method to remove the last element of the list. This is done by calling test_list.remove(test_list[-1]). test_list[-1] returns the last element of the list, which is 7. Then, remove() is called on the list to remove this element.
  4. Print the modified list using print("Modified list is : " + str(test_list)). This will output "Modified list is : [1, 4, 3, 6]".
Python3
# Python 3 code to demonstrate # Remove rear element # using list.remove()  # initializing list test_list = [1, 4, 3, 6, 7]  # Printing original list print("Original list is : " + str(test_list))  # using list.remove() to # Remove rear element test_list.remove(test_list[-1])  # Printing modified list print("Modified list is : " + str(test_list)) 

Output
Original list is : [1, 4, 3, 6, 7] Modified list is : [1, 4, 3, 6]

Time Complexity: O(n), The list.remove() method has a time complexity of O(n) in the worst case, where n is the number of elements in the list. This is because it needs to search through the list to find the element to remove.
Auxiliary Space: O(1) This approach does not use any additional auxiliary space since it modifies the original list in place.

Method 5: Using pop() with index

Step-by-step approach:

  • Initialize the list
  • Get the index of the last element using the len() method and subtracting 1
  • Remove the last element using the pop() method with the index as the argument
  • Print the modified list

Below is the implementation of the above approach:

Python3
# Python 3 code to demonstrate # Remove rear element # using pop() with index  # initializing list test_list = [1, 4, 3, 6, 7]  # Printing original list print("Original list is : " + str(test_list))  # Using pop() with index to remove last element test_list.pop(len(test_list)-1)  # Printing modified list print("Modified list is : " + str(test_list)) 

Output
Original list is : [1, 4, 3, 6, 7] Modified list is : [1, 4, 3, 6]

Time complexity: O(1) for the pop() method and O(n) for the len() method, but as n is constant, the time complexity can be considered as O(1).
Auxiliary space: O(1) as we are only removing the last element and not creating any new list or variable.


Next Article
Remove Multiple Elements from List in Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python | Remove random element from list
    Sometimes, while working with Python lists, we can have a problem or part of it, in which we desire to convert a list after deletion of some random element. This can have it's application in gaming domain or personal projects. Let's discuss certain way in which this task can be done. Method : Using
    3 min read
  • Python | Remove given element from the list
    Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
    7 min read
  • Python | Remove Front K elements
    We often come to situations in which we need to decrease the size of the list by truncating the first elements of the list. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar si
    7 min read
  • Remove Multiple Elements from List in Python
    In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list. [GFGTABS] Python a = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remov
    3 min read
  • Python | Remove given element from list of lists
    The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
    6 min read
  • Remove empty Lists from List - Python
    In this article, we will explore various method to remove empty lists from a list. The simplest method is by using a for loop. Using for loopIn this method, Iterate through the list and check each item if it is empty or not. If the list is not empty then add it to the result list. [GFGTABS] Python a
    2 min read
  • Remove an Element from a List by Index in Python
    We are given a list and an index value. We have to remove an element from a list that is present at that index value in a list in Python. In this article, we will see how we can remove an element from a list by using the index value in Python. Example: Input: [1, 2, 3, 4, 5], index = 2 Output: [1, 2
    3 min read
  • Remove last K elements of list - Python
    Given a list and an integer K, the task is to remove the last K elements from the list. For example, if the list is [1, 2, 3, 4, 5] and K = 2, the result should be [1, 2, 3]. Let’s explore different methods to remove the last K elements from a list in Python. Using list slicingList slicing is one of
    3 min read
  • Python - Append at Front and Remove from Rear
    We are given a list we need to append element in front and remove from rear. For example, a = [10, 20, 30] we need to add 5 in front and remove from rear so that resultant output should be [5, 10, 20]. Using insertUsing insert in Python allows us to add an element at the front of a list by specifyin
    2 min read
  • Python - Odd elements removal in List
    Due to the upcoming of Machine Learning, focus has now moved on handling the certain values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of certain values in essential and knowledge
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences