Back to home

Python Data Structures Handbook

Individual Method & Operation Reference Guide

List Data Structure Mutable Ordered

Detailed individual operational cards for all 18 List methods & syntax constructs.

1. Creation (Literals & list())

Creation

Instantiates a new list using square brackets or iterable conversion.

Syntax: lst = [] or list(iterable)
a = [1, 2, 3]
b = list("py")
print(a)
print(b)
[1, 2, 3]
['p', 'y']

2. List Comprehension

Creation

Concise syntax to construct lists based on existing iterables and filters.

Syntax: [expr for item in iterable if cond]
evens = [x for x in range(6) if x % 2 == 0]
print(evens)
[0, 2, 4]

3. Index Access

Accessing

Retrieves a single element at a zero-based positive or negative index.

Syntax: lst[index]
lst = ['a', 'b', 'c']
print(lst[0], lst[-1])
a c

4. Slicing

Accessing

Extracts a subset range of elements using start, stop, and step bounds.

Syntax: lst[start:stop:step]
nums = [10, 20, 30, 40, 50]
print(nums[1:4])
print(nums[::-1])
[20, 30, 40]
[50, 40, 30, 20, 10]

5. append()

Insertion

Appends a single element to the right end of the list in O(1) time.

Syntax: lst.append(element)
lst = [1, 2]
lst.append(3)
print(lst)
[1, 2, 3]

6. insert()

Insertion

Inserts an element at a specified index, shifting existing items to the right.

Syntax: lst.insert(index, element)
lst = [10, 30]
lst.insert(1, 20)
print(lst)
[10, 20, 30]

7. extend()

Insertion

Appends all elements from an iterable to the end of the list in-place.

Syntax: lst.extend(iterable)
lst = [1, 2]
lst.extend([3, 4])
print(lst)
[1, 2, 3, 4]

8. Concatenation (+)

Insertion

Combines two lists into a new third list using the + operator.

Syntax: new_lst = lst1 + lst2
a = [1, 2]
b = [3, 4]
res = a + b
print(res)
[1, 2, 3, 4]

9. Slice Assignment

Insertion

Replaces or inserts elements within a slice range in-place.

Syntax: lst[i:j] = iterable
lst = [1, 5]
lst[1:1] = [2, 3, 4]
print(lst)
[1, 2, 3, 4, 5]

10. remove()

Deletion

Removes the first matching element by value. Raises ValueError if absent.

Syntax: lst.remove(value)
lst = ['a', 'b', 'c']
lst.remove('b')
print(lst)
['a', 'c']

11. pop()

Deletion

Removes and returns an item at a specified index (defaults to the last item).

Syntax: item = lst.pop([index])
lst = [10, 20, 30]
val = lst.pop(1)
print("Popped:", val, "List:", lst)
Popped: 20 List: [10, 30]

12. del Statement

Deletion

Deletes items at a given index or slice, or deletes the whole variable.

Syntax: del lst[index] or del lst[i:j]
nums = [1, 2, 3, 4]
del nums[1:3]
print(nums)
[1, 4]

13. clear()

Deletion

Removes all items from the list, leaving an empty list.

Syntax: lst.clear()
lst = [10, 20]
lst.clear()
print(lst)
[]

14. sort()

Manipulation

Sorts list elements in ascending or descending order in-place.

Syntax: lst.sort(key=None, reverse=False)
nums = [3, 1, 4, 2]
nums.sort(reverse=True)
print(nums)
[4, 3, 2, 1]

15. reverse()

Manipulation

Reverses the order of list elements in-place.

Syntax: lst.reverse()
lst = [1, 2, 3]
lst.reverse()
print(lst)
[3, 2, 1]

16. count()

Utility

Returns total number of occurrences of a specified value.

Syntax: count_val = lst.count(value)
lst = [1, 2, 2, 3, 2]
print(lst.count(2))
3

17. index()

Utility

Returns index of first match of value. Raises ValueError if missing.

Syntax: idx = lst.index(value, [start, stop])
lst = ['a', 'b', 'c']
print(lst.index('b'))
1

18. copy()

Utility

Returns a shallow copy of the existing list.

Syntax: shallow_copy = lst.copy()
orig = [1, 2]
cp = orig.copy()
cp.append(3)
print("Orig:", orig, "Copy:", cp)
Orig: [1, 2] Copy: [1, 2, 3]