Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Array Slicing vs List Comprehension

Compare Python array slicing and list comprehension to decide when to select by position and when to filter or transform by value. See syntax, examples, and trade-offs.

~4 min readPython
#list slicing#list comprehension#python lists#python performance
Diagram comparing Python array slicing and list comprehension for list subsetting.

Python offers two common ways to build a new list from an existing one: list slicing and list comprehension. They solve different problems. Slicing selects items by position, while list comprehension filters or transforms items by value. The right choice depends on what you already know about the data and what you want the code to make clear.

What Slicing Does

Python's slice syntax list[start:stop:step] returns a new list containing elements from start up to but not including stop, moving by step. All three parts are optional. list[:] copies the whole list, list[::-1] reverses it, and list[::2] returns every second element. For straightforward positional extraction, slicing is fast because it avoids a Python-level loop.

python
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(numbers[2:6]) # [2, 3, 4, 5] print(numbers[::2]) # [0, 2, 4, 6, 8] print(numbers[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

The slice creates a new list containing references to the same elements. Modifying the new list does not affect the original list.

What List Comprehensions Do

A list comprehension builds a new list by applying an expression to each item in an iterable and optionally filtering with an if clause. Its general form is [expression for item in iterable if condition]. This is the idiomatic Python way to transform or filter a list.

python
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] squares = [x * x for x in numbers] evens = [x for x in numbers if x % 2 == 0]

Because the comprehension runs a Python-level loop for each item, it is more flexible than slicing but has more overhead when you only need a simple positional slice.

Where the Two Approaches Overlap

Some tasks can be expressed with either tool. For example, selecting every second element can be done with a slice or with a comprehension that checks the index.

python
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] every_second_slice = numbers[::2] every_second_comp = [x for i, x in enumerate(numbers) if i % 2 == 0]

Both produce [0, 2, 4, 6, 8]. The slice is shorter and faster because it avoids the Python loop. But the comprehension version can also check the element's value, something a positional slice cannot do directly. When the selection depends on both position and value, a comprehension with enumerate is the only one of these two tools that can express it.

Key Differences: Position vs Value

The fundamental difference is that slicing selects by position, while list comprehension selects or transforms by value. Slicing cannot ask for all numbers greater than 5. A comprehension cannot ask for elements from index 2 to 5 without extra logic such as enumerate; [x for i, x in enumerate(data) if 2 <= i <= 5] works, but data[2:6] expresses the same intent more directly.

In practice, choose based on what you know about the data: if you know the positions, use slicing; if you need to evaluate each element, use comprehension.

Performance and Memory Behavior

Slicing copies references from the original list into a new list in one compact operation. For a simple contiguous subsequence, it is usually the fastest way to get a new list. A list comprehension runs a Python-level loop, so it is slower for the same output, but it may be the only direct approach when filtering or transformation is required.

Both operations allocate a new list. Slicing copies references, not the elements themselves, so the memory cost is proportional to the number of references in the slice. A comprehension also allocates a new list and evaluates the expression for each item. If you only need to iterate over a slice without storing it, consider itertools.islice to avoid the copy, but that is a separate optimization.

Readability and Intent

Code clarity often matters more than micro-optimization. A slice like data[3:7] immediately tells the reader that you want a contiguous block of items. A comprehension like [x for x in data if x > 0] tells the reader that you are filtering by value. Using slicing for a value-based filter, or a comprehension for a simple positional range, makes the code harder to understand. Match the tool to the intent.

When to Use Slicing

Use slicing when you need:

  • a contiguous subsequence, such as data[start:end]
  • a step pattern, like every other element or a reversed list
  • a shallow copy of the list, via data[:]
  • to remove a slice in place with del data[start:end]

Slicing is also the natural way to work with fixed-size windows in algorithms that process sequences.

When to Use List Comprehension

Use list comprehension when you need:

  • to filter elements by a value-based condition
  • to transform each element, such as converting types or applying a function
  • to combine multiple conditions or nested loops
  • to build a list from another iterable, not just a list

For example, [int(s) for s in strings] converts a list of strings to integers. Complex validation may be clearer in a helper function, but the comprehension still provides the list-building part.

Common Mistakes and Edge Cases

One common mistake is assuming slicing behaves the same way when the underlying data is a NumPy array. In NumPy, basic slicing such as arr[1:4] returns a view, not a copy, so modifying the slice changes the original array. Python lists always copy the references when sliced, so the behavior differs. If you work with both, keep that distinction in mind.

Another edge case is slicing beyond the list bounds. Python silently truncates the slice, so data[5:100] returns the elements from index 5 to the end, not an error. A comprehension with an index condition would need explicit bounds checking.

Finally, remember that both slicing and list comprehension create a new list. If you change the contents of the original list after creating the new list, the new list keeps the references it copied or produced at creation time. This is usually what you want, but it can be surprising when you expect a view.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.