Python - Column deletion from list of lists

In a list of lists, an element at the same index of each sublist represents a column-like structure. In this article we will see how to delete a column from a list of lists, which means deleting the element at the same index position from each sublist.

Using pop() Method

The pop() method removes and returns the element at a specific index. We can use list comprehension to apply pop() to each sublist ?

# List of lists representing tabular data
data = [[3, 9, 5, 1],
        [4, 6, 1, 2],
        [1, 6, 12, 18]]

print("Original list:")
print(data)

# Remove column at index 2 using pop()
[row.pop(2) for row in data]

print("\nAfter deleting column at index 2:")
print(data)
Original list:
[[3, 9, 5, 1], [4, 6, 1, 2], [1, 6, 12, 18]]

After deleting column at index 2:
[[3, 9, 1], [4, 6, 2], [1, 6, 18]]

Using del Statement

The del statement directly removes an element at the specified index without returning it. This approach uses a for loop to iterate through each sublist ?

# List of lists representing tabular data
data = [[3, 9, 5, 1],
        [4, 6, 1, 2],
        [1, 6, 12, 18]]

print("Original list:")
print(data)

# Remove column at index 2 using del
for row in data:
    del row[2]

print("\nAfter deleting column at index 2:")
print(data)
Original list:
[[3, 9, 5, 1], [4, 6, 1, 2], [1, 6, 12, 18]]

After deleting column at index 2:
[[3, 9, 1], [4, 6, 2], [1, 6, 18]]

Using List Comprehension with Slicing

This approach creates a new list by excluding the specified column index, preserving the original data ?

# List of lists representing tabular data
data = [[3, 9, 5, 1],
        [4, 6, 1, 2],
        [1, 6, 12, 18]]

print("Original list:")
print(data)

# Create new list excluding column at index 2
column_to_delete = 2
result = [row[:column_to_delete] + row[column_to_delete + 1:] for row in data]

print(f"\nAfter removing column at index {column_to_delete}:")
print(result)
print(f"\nOriginal data unchanged:")
print(data)
Original list:
[[3, 9, 5, 1], [4, 6, 1, 2], [1, 6, 12, 18]]

After removing column at index 2:
[[3, 9, 1], [4, 6, 2], [1, 6, 18]]

Original data unchanged:
[[3, 9, 5, 1], [4, 6, 1, 2], [1, 6, 12, 18]]

Comparison

Method Modifies Original Returns Value Best For
pop() Yes Yes (removed elements) When you need the deleted values
del Yes No Simple column deletion
List comprehension No New list Preserving original data

Conclusion

Use pop() when you need the deleted values, del for simple in-place deletion, or list comprehension to preserve the original data. All methods effectively remove columns from list of lists.

Updated on: 2026-03-15T18:15:58+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements