Learn Python
>>> data_structures.list_methods_slicing

List Methods & Slicing in Python

#concept

Lists support a wide set of built-in methods for adding, removing, sorting, and rearranging items, plus the same slicing syntax used on strings for extracting a sub-list by position. Because lists are mutable, many of these methods change the list directly rather than returning a new one — worth watching closely, since it's a common source of surprises.

#how it works
1

Slicing works the same as strings

list[start:stop:step] extracts a range of items.

2

Adding methods modify in place

.append(), .insert(), and .extend() all change the original list and return None.

3

Removing methods modify in place too

.remove() and .pop() change the original — .pop() additionally returns the removed item.

4

.sort() vs sorted()

.sort() rearranges the list in place; the built-in sorted() returns a new sorted list, leaving the original untouched.

5

.copy() avoids shared references

Creates a genuinely separate list, rather than a second name pointing at the same one.

#example
nums = [3, 1, 4, 1, 5]
print(nums[1:4])
 
nums.append(9)
print(nums)
 
nums.sort()
print(nums)
Output
[1, 4, 1]
[3, 1, 4, 1, 5, 9]
[1, 1, 3, 4, 5, 9]
1nums[1:4] slices out items at index 1, 2, and 3, giving [1, 4, 1].
2.append(9) adds 9 to the end of the list, in place — the same nums object is modified.
3.sort() rearranges the list in place, in ascending order by default.
#variants

Adding Items

.append() adds one item to the end; .insert() adds at a specific position; .extend() adds every item from another iterable.

nums = [1, 2]
nums.insert(1, 99)
print(nums)
Output
[1, 99, 2]

Removing Items

.remove() deletes the first matching value; .pop() removes and returns an item by index (the last one, by default).

nums = [1, 2, 3]
last = nums.pop()
print(last, nums)
Output
3 [1, 2]

sort() vs sorted()

.sort() rearranges the list in place and returns None; the built-in sorted() leaves the original untouched and returns a brand new sorted list.

nums = [3, 1, 2]
result = sorted(nums)
print(result, nums)
Output
[1, 2, 3] [3, 1, 2]

Copying a List

Assigning with = just creates a second name pointing at the same list; .copy() (or list(original)) creates a genuinely separate list.

original = [1, 2, 3]
copy = original.copy()
copy.append(4)
print(original, copy)
Output
[1, 2, 3] [1, 2, 3, 4]
#complexity
time Varies by operation: append O(1), insert/remove O(n), sort O(n log n)
space O(1) extra for most operations; sort() uses O(n) internally

append() is fast because it just adds to the end, but insert() and remove() may need to shift every following item over by one position, making them O(n) in the worst case.

#use cases
Building and maintaining a dynamic collection that changes throughout a program's life
Sorting data for display, like a leaderboard or alphabetized results
Safely duplicating a list before modifying it, to avoid unexpectedly changing the original
Extracting a specific sub-range of a larger list with slicing, like the first 10 results

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.