A list in Python is an ordered, mutable collection that can store elements of mixed types.
tech_stack = ['Python', 'Go', 'Rust', 'Java', 'Kotlin']
print(tech_stack)
Output:
['Python', 'Go', 'Rust', 'Java', 'Kotlin']
The type() function confirms the object class:
tech_stack = ['Python', 'Go', 'Rust', 'Java', 'Kotlin']
print(type(tech_stack))
Output:
<class 'list'>
Indexing
Each element occupies a sequential slot identified by an integer offset. For the list above, valid offsets start at 0 and map to memory positions as follows:
| 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| Python | Go | Rust | Java | Kotlin |
Negative offsets provide reverse access:
| -5 | -4 | -3 | -2 | -1 |
|---|---|---|---|---|
| Python | Go | Rust | Java | Kotlin |
Retrieve individual items with bracket notation:
tech_stack = ['Python', 'Go', 'Rust', 'Java', 'Kotlin']
print(tech_stack[0])
print(tech_stack[2])
print(tech_stack[4])
Output:
Python
Rust
Kotlin
Using negative indices:
tech_stack = ['Python', 'Go', 'Rust', 'Java', 'Kotlin']
print(tech_stack[-1])
print(tech_stack[-3])
Output:
Kotlin
Rust
Attempting to read beyond the last valid offset raises an IndexError:
tech_stack = ['Python', 'Go', 'Rust', 'Java', 'Kotlin']
print(tech_stack[5])
Output:
IndexError: list index out of range
Slicing
Slicing extracts subsequences through the syntax:
sequence[start:stop:step]
The range includes start and excludes stop. When step is omitted, it defaults to 1.
Examples with positive step:
tech_stack = ['Python', 'Go', 'Rust', 'Java', 'Kotlin']
print(tech_stack[0:3])
print(tech_stack[:3])
print(tech_stack[3:])
print(tech_stack[1:-1])
print(tech_stack[::2])
Output:
['Python', 'Go', 'Rust']
['Python', 'Go', 'Rust']
['Java', 'Kotlin']
['Go', 'Rust', 'Java']
['Python', 'Rust', 'Kotlin']
A negative step reverses traversal; in this case start should logically precede stop in the right-to-left direction:
tech_stack = ['Python', 'Go', 'Rust', 'Java', 'Kotlin']
print(tech_stack[3:0:-1])
print(tech_stack[::-1])
Output:
['Java', 'Rust', 'Go']
['Kotlin', 'Java', 'Rust', 'Go', 'Python']
Iteration
for loops offer direct element access:
tech_stack = ['Python', 'Go', 'Rust', 'Java', 'Kotlin']
for item in tech_stack:
print(item)
Output:
Python
Go
Rust
Java
Kotlin
while loops require a manual counter:
tech_stack = ['Python', 'Go', 'Rust', 'Java', 'Kotlin']
idx = 0
while idx < len(tech_stack):
print(tech_stack[idx])
idx += 1
Output:
Python
Go
Rust
Java
Kotlin
Mutating Lists
Appending Items
append() adds a single element to the end:
scores = [85, 90, 78]
scores.append(92)
print(scores)
Output:
[85, 90, 78, 92]
insert() places an element at an arbitrary offset, shifting existing items right:
scores = [85, 90, 78]
scores.insert(1, 88)
print(scores)
Output:
[85, 88, 90, 78]
extend() unpacks another iterable into the tail of the list:
group_a = ['alpha', 'beta']
group_b = ['gamma', 'delta']
group_a.extend(group_b)
print(group_a)
Output:
['alpha', 'beta', 'gamma', 'delta']
Updating Items
Assignment via index overwrites the existing value:
scores = [85, 90, 78]
scores[0] = 95
print(scores)
Output:
[95, 90, 78]
Searching Items
Membership operators test existance without knowing the index:
scores = [85, 90, 78]
print(100 in scores)
print(100 not in scores)
Output:
False
True
count() tallies occurrences of a specific value:
readings = [22, 24, 22, 26, 22]
print(readings.count(22))
Output:
3
Removing Items
del drops an element by offset without returning it:
readings = [22, 24, 22, 26]
del readings[0]
print(readings)
Output:
[24, 22, 26]
pop() removes an element by offset and yields the removed value; omitting the argument targets the final item:
readings = [22, 24, 22, 26]
last = readings.pop()
print(last, readings)
first = readings.pop(0)
print(first, readings)
Output:
26 [22, 24, 22]
22 [24, 22]
remove() targets the first matching value rather than a position:
readings = [22, 24, 22, 26]
readings.remove(24)
print(readings)
Output:
[22, 22, 26]
Ordering Items
sort() arranges elements in place. The reverse flag controls direction:
values = [3, 1, 4, 1, 5]
values.sort()
print(values)
values.sort(reverse=True)
print(values)
Output:
[1, 1, 3, 4, 5]
[5, 4, 3, 1, 1]
reverse() simply flips the current sequence order without sorting:
values = [3, 1, 4, 1, 5]
values.reverse()
print(values)
Output:
[5, 1, 4, 1, 3]