Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Create and Check an Empty List in Python

  • 29 Mar, 2025
  • Com 0
How to create and check an empty list in Python

What do you think of when you hear “an empty list?” Your first thought would be a list with zero elements. Its length is 0. It still consumes memory because of the underlying object structure and memory allocation strategies.

Empty listCreating an empty list

The most optimal way to initialize an empty list is to use square brackets ([ ]) with nothing in them, and assign it to a variable, like main_list = []. Why is it efficient? Because it does not require any function calling. So, no overhead.

Creating an empty list using []

empty_list = []

print(empty_list)
# Output: []

print(type(empty_list))
# <class 'list'>

Alternative: list() Constructor

Another way is the list() constructor, which also creates an empty list. However, it is slightly lower than [] because it requires a global lookup and function call.

Using list() constructor

another_empty_list = list()

print(another_empty_list)
# Output: []

print(type(another_empty_list))
# Output: <class 'list'>

Pre-allocating list size

Lists do not require preallocation, and in some cases, it is not considered the best approach. However, if you want, you can initialize with placeholders like None, if needed:

preallocated_list = [None] * 1000

# Output: A list with 1000 None elements like this [None, None,.....None]

Checking if a list is empty

Checking if a list is empty in Python

The most efficient way to check if a list is empty is to use the implicit boolean check using the not operator with an if statement. If a list is empty, it returns False, so using the not operator, it returns True.

This leverages Python’s truth value testing. The time and space complexity for checking emptiness is O(1) because it is a direct evaluation of its internal state, which is a constant time.

empty_list = []

if not empty_list:
    print("List is empty")
  
# Output: List is empty

Alternative 1: Comparing length with 0

There is a built-in len() function to check the list’s length, and if it is 0, it is empty. Since it requires a function call, it is marginally slower than an “if not” expression.

empty_list = []

if len(empty_list) == 0:
    print("List is empty")

# Output: List is empty

Alternative 2: Comparing our list to [] (Not Recommended)

Using the == operator, you can compare two variables and check if they are the same. For example, we can check empty_list == []. Logically, it works and returns the correct output.

Still, it is not an efficient approach because it creates a new empty list for comparison, which incurs overhead when performance is critical.

Avoid the “is” operator for comparison

Another problematic approach is using the “is” operator to check for emptiness because it checks object identity, not content.

empty_list = []

if empty_list is []:
  print("List is empty")
else:
  print("List is not empty")

# Output: List is not empty

You can see from the above code and its output that even if the list is empty, we get the output that says, “List is not empty”. Why? Because it does not check the list’s content. Instead, it compares the two list objects, which are fundamentally different.

Type-specific checks

While checking if you are unsure of the object you are working with, you can use the built-in isinstance() method like this:

empty_list = []

if isinstance(empty_list, list) and not empty_list:
    print("An empty list.")
else:
    print("Not an empty list.")

# Output: An empty list.

That’s all!

Post Views: 35
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Create and Check an Empty String in Python
How to Create and Check Empty DataFrame in Pandas

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend