List Methods Reference
Adding, deleting, and slice transformations on lists.
Add an element to the end of the list.
Used In: Accumulating results, batch processing.
list.append(x)lst = [1, 2]
lst.append(3)
print(lst)[1, 2, 3]Time Complexity: O(1) amortized
Common Mistakes: Writing x = lst.append(3) assigns None to x because append() does not return the list.
Related Methods: extend(), insert()
append() vs extend(): append() adds its argument as a single element to the end of the list; extend() iterates over its argument, appending each item to the list.
Append elements from an iterable (like another list) to the current list.
Used In: Flattening nested lists, accumulating batch feeds.
list.extend(iterable)lst = [1, 2]
lst.extend([3, 4])
print(lst)[1, 2, 3, 4]Time Complexity: O(K) where K is elements count in iterable
Related Methods: append()
append() vs extend(): extend() flattens the items of the input list; append() adds the list object itself.
Insert element at a specific index, shifting subsequent elements.
Used In: Injecting values at controlled positions.
list.insert(index, x)lst = [10, 30]
lst.insert(1, 20)
print(lst)[10, 20, 30]Time Complexity: O(N)
Related Methods: append()
Remove the first occurrence of a value from the list.
Used In: Deleting specific entries from collections.
list.remove(x)lst = ['a', 'b', 'a']
lst.remove('a')
print(lst)['b', 'a']Time Complexity: O(N)
Related Methods: pop()
Remove and return item at index (default last).
Used In: Stack operations, queue consumption.
list.pop(index=-1)lst = [10, 20, 30]
val = lst.pop(0)
print(val, lst)10 [20, 30]Time Complexity: O(1) for last, O(N) for index 0 (requires shifting elements)
Common Mistakes: Popping from an empty list raises IndexError.
Related Methods: remove(), del
remove() vs pop(): remove(x) searches for the first occurrence of value x and deletes it in O(N) without returning it; pop(i) removes and returns the item at index i in O(N) (or O(1) if last).
Remove all elements from the list, making it empty.
Used In: Emptying batches after processing.
list.clear()lst = [1, 2]
lst.clear()
print(lst)[]Time Complexity: O(1)
Related Methods: del
Return a shallow copy of the list.
Used In: Creating safe isolated lists for sorting.
list.copy()lst = [1, [2]]
dup = lst.copy()
dup[1].append(3)
print(lst)[1, [2, 3]]Time Complexity: O(N)
Related Methods: copy.deepcopy()
copy() vs copy.deepcopy(): copy() performs a shallow copy; deepcopy() recursively copies all nested elements.
Sort the elements of the list in-place.
Used In: Ordering datasets, sorting log events.
list.sort(key=None, reverse=False)lst = [3, 1, 2]
lst.sort()
print(lst)[1, 2, 3]Time Complexity: O(N log N) (Timsort)
Related Methods: sorted()
sort() vs sorted(): sort() modifies the list in-place and returns None; sorted() returns a new sorted list, leaving the original unchanged.
Reverse the elements of the list in-place.
Used In: Stack processing.
list.reverse()lst = [1, 2, 3]
lst.reverse()
print(lst)[3, 2, 1]Time Complexity: O(N)
Related Methods: reversed()
Return index of the first occurrence of a value.
Used In: Locating specific offsets.
list.index(x, start=0, end=len(list))['a', 'b', 'c'].index('b')1Time Complexity: O(N)
Related Methods: find()
Return count of occurrences of a value in the list.
Used In: Frequency checks, quick validation.
list.count(x)[1, 2, 1, 1].count(1)3Time Complexity: O(N)
Related Methods: collections.Counter