Python Study Notes – Lists
1. Introduction
● A list is an ordered, mutable (changeable) collection of items.
● Elements can be of different data types.
● Defined using square brackets [].
numbers = [10, 20, 30, 40]
mixed = [1, "Hello", 3.5, True]
2. Indexing
● Lists use 0-based indexing.
● Negative indexing allowed.
L = [10, 20, 30, 40]
print(L[0]) # 10
print(L[-1]) # 40
3. List Operations
1. Concatenation (+) → [1, 2] + [3, 4] → [1, 2, 3, 4]
2. Repetition (*) → [1, 2] * 2 → [1, 2, 1, 2]
3. Membership (in) → 3 in [1,2,3] → True
Slicing →
L = [10, 20, 30, 40, 50]
print(L[1:4]) # [20, 30, 40]
print(L[:3]) # [10, 20, 30]
print(L[-2:]) # [40, 50]
4.
4. Traversing a List
Using loops to access elements.
for x in [10, 20, 30]:
print(x)
5. Built-in List Functions & Methods
● len(L) → number of elements
● list(seq) → converts into list
● append(x) → add element at end
● extend(iterable) → add multiple elements
● insert(i, x) → insert at index
● count(x) → number of occurrences
● index(x) → first index of element
● remove(x) → delete first occurrence
● pop(i=-1) → remove element by index (default last)
● reverse() → reverse list in place
● sort() → sort list (ascending by default)
● sorted(L) → return sorted copy
● min(L), max(L), sum(L) → min, max, sum of elements
6. Nested Lists
● A list inside another list.
matrix = [[1, 2], [3, 4], [5, 6]]
print(matrix[1][0]) # 3
7. Suggested Programs
1. Find maximum, minimum, mean
nums = [10, 25, 7, 32]
print("Max =", max(nums))
print("Min =", min(nums))
print("Mean =", sum(nums)/len(nums))
2. Linear Search
nums = [4, 7, 2, 9, 5]
key = 9
found = False
for i in range(len(nums)):
if nums[i] == key:
print("Found at index", i)
found = True
break
if not found:
print("Not found")
3. Count frequency of elements
nums = [1, 2, 2, 3, 1, 4, 2]
freq = {}
for x in nums:
freq[x] = [Link](x, 0) + 1
print(freq) # {1:2, 2:3, 3:1, 4:1}
📌 Quick Revision
● Lists are mutable, ordered sequences.
● Indexing → 0-based, supports negative.
● Operations → Concatenation (+), Repetition (*), Membership (in), Slicing.
● Key Functions → append(), extend(), insert(), remove(), pop(), reverse(), sort(),
sorted(), min(), max(), sum().
● Nested Lists → list of lists.
● Practice Programs → max/min/mean, linear search, frequency count.