Lists
Checking access...
Lists are ordered, mutable sequences — Python’s workhorse collection type.
Creating Lists
# Empty listempty = []empty = list()
# With initial valuesnumbers = [1, 2, 3, 4, 5]mixed = [1, "hello", 3.14, True]nested = [[1, 2], [3, 4]]
# From other sequenceslist("hello") # ['h', 'e', 'l', 'l', 'o']list(range(5)) # [0, 1, 2, 3, 4]Indexing and Slicing
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
# Indexingfruits[0] # 'apple'fruits[-1] # 'elderberry'fruits[-2] # 'date'
# Slicingfruits[1:3] # ['banana', 'cherry']fruits[:3] # ['apple', 'banana', 'cherry']fruits[::2] # ['apple', 'cherry', 'elderberry']fruits[::-1] # ['elderberry', 'date', 'cherry', 'banana', 'apple']List Methods
items = [1, 2, 3]
# Addingitems.append(4) # [1, 2, 3, 4]items.insert(0, 0) # [0, 1, 2, 3, 4]items.extend([5, 6]) # [0, 1, 2, 3, 4, 5, 6]
# Removingitems.remove(3) # removes first 3 — ValueError if not foundpopped = items.pop() # removes and returns last item (6)first = items.pop(0) # removes and returns item at index 0
# Findingitems = [10, 20, 30, 20, 40]items.index(20) # 1 — first occurrenceitems.index(20, 2) # 3 — starting from index 2items.count(20) # 2
# Reorderingitems.sort() # sorts in place — [10, 20, 20, 30, 40]items.sort(reverse=True) # [40, 30, 20, 20, 10]items.reverse() # reverses in place
# Copyingcopy = items.copy() # shallow copydeep = items[:] # slice copy — also shallowCaution
Methods like .sort(), .reverse(), and .append() modify the list in place and return None. Methods like .copy() return a new list.
List Comprehensions
# Basicsquares = [x ** 2 for x in range(10)]# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# With conditionevens = [x for x in range(20) if x % 2 == 0]
# With if-elselabels = ["even" if x % 2 == 0 else "odd" for x in range(5)]# ['even', 'odd', 'even', 'odd', 'even']
# Nestedmatrix = [[i + j for j in range(3)] for i in range(3)]# [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
# Flatten a nested listnested = [[1, 2], [3, 4], [5, 6]]flat = [item for sublist in nested for item in sublist]# [1, 2, 3, 4, 5, 6]List Operations
# Concatenation and repetition[1, 2] + [3, 4] # [1, 2, 3, 4][1] * 4 # [1, 1, 1, 1]
# Membership3 in [1, 2, 3] # True5 not in [1, 2, 3] # True
# Length, min, max, sumlen([1, 2, 3]) # 3min([3, 1, 2]) # 1max([3, 1, 2]) # 3sum([1, 2, 3]) # 6Sorting with sorted()
The sorted() function returns a new sorted list:
nums = [3, 1, 4, 1, 5]sorted(nums) # [1, 1, 3, 4, 5]sorted(nums, reverse=True) # [5, 4, 3, 1, 1]
# Sort by key functionwords = ["apple", "kiwi", "banana", "pear"]sorted(words, key=len) # ['pear', 'kiwi', 'apple', 'banana']
# Sort by last charactersorted(words, key=lambda w: w[-1])Lists vs. Arrays
Native Python lists are flexible but can be memory-heavy for numeric data. For numerical work, use array from the standard library or numpy:
from array import arraynums = array("i", [1, 2, 3]) # type 'i' = signed intKey Takeaways
- Lists are ordered, mutable, and can hold mixed types
- Indexing starts at 0; negative indexes count from the end
- Slice:
list[start:stop:step] - List comprehensions:
[expr for item in iterable if condition] .sort()sorts in place;sorted()returns a new list.append(),.extend(),.pop(),.remove()modify in place- Use
.copy()or[:]for shallow copies