
- 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
Python - Column deletion from list of lists
In a list of lists an element at the same index of each of the sublist represents a column like structure. In this article we will see how we can delete a column from a list of lists. Which means we have to delete the element at the same index position from each of the sublist.
Using pop
We use the pop method which removes the element at a particular position. A for loop is designed to iterate through elements at specific index and removes them using pop.
Example
# List of lists listA = [[3, 9, 5, 1], [4, 6, 1, 2], [1, 6, 12, 18]] # printing original list print("Given list \n",listA) # Apply pop [i.pop(2) for i in listA] # Result print("List after deleting the column :\n ",listA)
Output
Running the above code gives us the following result −
Given list [[3, 9, 5, 1], [4, 6, 1, 2], [1, 6, 12, 18]] List after deleting the column : [[3, 9, 1], [4, 6, 2], [1, 6, 18]]
With del
In this approach we use the del function which is similar to above approach. We mention the index at which the column has to be deleted.
Example
# List of lists listA = [[3, 9, 5, 1], [4, 6, 1, 2], [1, 6, 12, 18]] # printing original list print("Given list \n",listA) # Apply del for i in listA: del i[2] # Result print("List after deleting the column :\n ",listA)
Output
Running the above code gives us the following result −
Given list [[3, 9, 5, 1], [4, 6, 1, 2], [1, 6, 12, 18]] List after deleting the column : [[3, 9, 1], [4, 6, 2], [1, 6, 18]]
- Related Articles
- Python - Convert column to separate elements in list of lists
- Get positive elements from given list of lists in Python
- Python – Filter rows with only Alphabets from List of Lists
- Convert list into list of lists in Python
- Python - Convert List of lists to List of Sets
- Python Front and rear range deletion in a list?
- Program to interleave list elements from two linked lists in Python
- How to join list of lists in python?
- Python - Ways to iterate tuple list of lists
- Custom Multiplication in list of lists in Python
- Get maximum of Nth column from tuple list in Python
- Find common elements in list of lists in Python
- Checking triangular inequality on list of lists in Python
- Convert a list into tuple of lists in Python
- Check if a list exists in given list of lists in Python

Advertisements