
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What is the best way to handle list empty exception in Python?
List is an ordered sequence of elements. Individual element in list is accessed using index starting with 0 and goes up to length-1. If index goes beyond this range, IndexError exception is encountered.
In following example, an infinite loop is used to pop one element at a time. As loop tries to go even after last element is popped, IndexError exception will be encountered. We trap it using try – except mechanism.
a=[1,2,3] while True: try: b=a.pop() print (b) except (IndexError): break
- Related Articles
- What is best way to check if a list is empty in Python?
- What is the best way to log a Python exception?
- What is the best way to handle a Javascript popup using Selenium Webdriver?
- How to handle an exception in Python?
- How to handle Python exception in Threads?
- What is the best way to learn Python and Django?
- What is the best way to get stock data using Python?
- What is the Best Way to Develop Desktop Applications Using Python?
- What is the most elegant way to check if the string is empty in Python?
- What is the best way to run all Python files in a directory?
- What is the correct way to pass an object with a custom exception in Python?
- What is the best way to search for an item in a sorted list in JavaScript?
- What are the best practices for exception handling in Python?
- How to handle python exception inside if statement?
- What is the best way to check capacity in Java?

Advertisements