List Data Structure Mutable Ordered
Detailed individual operational cards for all 18 List methods & syntax constructs.
1. Creation (Literals & list())
CreationInstantiates a new list using square brackets or iterable conversion.
a = [1, 2, 3]
b = list("py")
print(a)
print(b)
['p', 'y']
2. List Comprehension
CreationConcise syntax to construct lists based on existing iterables and filters.
evens = [x for x in range(6) if x % 2 == 0]
print(evens)
3. Index Access
AccessingRetrieves a single element at a zero-based positive or negative index.
lst = ['a', 'b', 'c']
print(lst[0], lst[-1])
4. Slicing
AccessingExtracts a subset range of elements using start, stop, and step bounds.
nums = [10, 20, 30, 40, 50]
print(nums[1:4])
print(nums[::-1])
[50, 40, 30, 20, 10]
5. append()
InsertionAppends a single element to the right end of the list in O(1) time.
lst = [1, 2]
lst.append(3)
print(lst)
6. insert()
InsertionInserts an element at a specified index, shifting existing items to the right.
lst = [10, 30]
lst.insert(1, 20)
print(lst)
7. extend()
InsertionAppends all elements from an iterable to the end of the list in-place.
lst = [1, 2]
lst.extend([3, 4])
print(lst)
8. Concatenation (+)
InsertionCombines two lists into a new third list using the + operator.
a = [1, 2]
b = [3, 4]
res = a + b
print(res)
9. Slice Assignment
InsertionReplaces or inserts elements within a slice range in-place.
lst = [1, 5]
lst[1:1] = [2, 3, 4]
print(lst)
10. remove()
DeletionRemoves the first matching element by value. Raises ValueError if absent.
lst = ['a', 'b', 'c']
lst.remove('b')
print(lst)
11. pop()
DeletionRemoves and returns an item at a specified index (defaults to the last item).
lst = [10, 20, 30]
val = lst.pop(1)
print("Popped:", val, "List:", lst)
12. del Statement
DeletionDeletes items at a given index or slice, or deletes the whole variable.
nums = [1, 2, 3, 4]
del nums[1:3]
print(nums)
13. clear()
DeletionRemoves all items from the list, leaving an empty list.
lst = [10, 20]
lst.clear()
print(lst)
14. sort()
ManipulationSorts list elements in ascending or descending order in-place.
nums = [3, 1, 4, 2]
nums.sort(reverse=True)
print(nums)
15. reverse()
ManipulationReverses the order of list elements in-place.
lst = [1, 2, 3]
lst.reverse()
print(lst)
16. count()
UtilityReturns total number of occurrences of a specified value.
lst = [1, 2, 2, 3, 2]
print(lst.count(2))
17. index()
UtilityReturns index of first match of value. Raises ValueError if missing.
lst = ['a', 'b', 'c']
print(lst.index('b'))
18. copy()
UtilityReturns a shallow copy of the existing list.
orig = [1, 2]
cp = orig.copy()
cp.append(3)
print("Orig:", orig, "Copy:", cp)