Together with dictionaries, Python lists are among the most often used data structures. A Python list is more than simply a list; it may also be used as a stack or a queue. In this tutorial, I will walk you through the process of make and remove an Empty list in Python.
In Python, what is an empty list?
Lists in Python are similar to arrays, but with the added benefit of including heterogeneous objects, making lists an excellent Python tool. As a result, a list can include a variety of data types such as Integer, String, and Object.
How do you make an empty list?
There are two ways to build an empty list in Python. To begin, the empty square bracket notation [] generates a new list object with no elements. Second, the no-argument list() constructor also returns an empty list object.
Method 1: Make use of [ ]
In Python, we may make an empty list by adding square brackets to a variable.
# Creating an empty list with the name "data" data = []
Code:
# Creating an empty list with the name "data" data = [] print("Length of List:",len(data)) print("bool(data):",bool(data))
Output:
Length of List: 0 bool(data): False
Explanation:
When the list of data is passed in both circumstances in the above example, the data in Python is an empty list.
Now that we’ve built an empty list in Python, let’s add some items to it using the append() function.
Code:
# Creating an empty list with the name "data" data = [] # Inserting elements in the list data.append("Python") data.append("is a") data.append("Programming") data.append("Language") print("Items of list are:", data) print("Length of List:",len(data))
Output:
Items of list are: ['Python', 'is a', 'Programming', 'Language'] Length of List: 4
Explanation:
The preceding example demonstrates how to use Python’s append() function to add items to an empty list (formed with [] ).
Method 2: Make use of a list ()
The list() constructor is another way to build an empty list in Python. It is used to generate a list, and if no argument is supplied to the list() constructor, it will produce an empty list, according to the Python documentation.
Code:
# Creating an empty list with the name "data" data = list() print("Length of List:",len(data)) print("bool(data):",bool(data))
Output:
Length of List: 0 bool(data): False
Explanation:
As shown in the result, the length of the list is 0 and the bool context of the list is false. As a result, data is an empty list.
Let’s now append some entries to the list using Python’s append() technique.
Code:
# Creating an empty list with the name "data" data = list() # Inserting elements in the list data.append("Python") data.append("is a") data.append("Programming") data.append("Language") print("Items of list are:", data) print("Length of List:",len(data))
Output:
Items of list are: ['Python', 'is a', 'Programming', 'Language'] Length of List: 4
Explanation:
The below example demonstrates how to append items to an empty list using Python’s append() function.
How do you remove an empty list?
Method 1: Employ a list comprehension
This is one solution to this problem. We cycle through the list, except the empty list.
Example:
# Python3 code to Demonstrate Remove empty List # from List using list comprehension # Initializing list test_list = [5, 6, [], 3, [], [], 9] # original printing list print("The original list is : " + str(test_list)) # Remove empty List from List # using list comprehension res = [ele for ele in test_list if ele != []] # printing result print("List after empty list removal : " + str(res))
Output:
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2: Using filter() method
Here’s another method to complete this assignment. We filter None in this area. None values include empty lists, which will be eliminated.
Example:
# Python3 Code to Demonstrate Remove empty List # from List using filter() Method # Initializing list by custom values test_list = [5, 6, [], 3, [], [], 9] # Printing original list print("The original list is : " + str(test_list)) # Removing empty List from List # using filter() method res = list(filter(None, test_list)) # Printing the resultant list print("List after empty list removal : " + str(res))
Output:
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 3: Using function definition
Example
# Python Code to Remove empty List from List def empty_list_remove(input_list): new_list = [] for ele in input_list: if ele: new_list.append(ele) return new_list # input list values input_list = [5, 6, [], 3, [], [], 9] # print initial list values print(f"The original list is : {input_list}") # function-call & print values print(f"List after empty list removal : {empty_list_remove(input_list)}")
Output:
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
The above-mentioned procedure (Method 3) is the most optimal of the three.
Conclusion
In summary, this article has given you ways to create and remove empty list in list using python. We hope to have provided you with complete information and the most satisfying implementation step.
At ONEXTDIGITAL hires a team of qualified and talented developers to give excellent Web/Mobile Application Development. We guarantee that we will delight you completely.
Read also