PyForge · Python practice library
All topics

Topic 06 of 17

Lists

Ordered, mutable sequences — the workhorse collection in Python.

Indexing & slicing
append/extend/pop
List comprehensions
Sorting

Basic

2 programs

Program 1

List operations

Basic

Common list methods.

python
nums = [3, 1, 4, 1, 5, 9, 2, 6]
nums.append(5)
nums.sort()
print(nums)
print("Max:", max(nums), "Min:", min(nums))

Program 2

Sum and average

Basic

Aggregate numbers in a list.

python
marks = [78, 85, 92, 67, 88]
print("Total:", sum(marks))
print("Average:", sum(marks) / len(marks))

Intermediate

2 programs

Program 1

List comprehension

Intermediate

Squares of even numbers only.

python
squares = [x * x for x in range(1, 11) if x % 2 == 0]
print(squares)

Output

[4, 16, 36, 64, 100]

Program 2

Remove duplicates (preserve order)

Intermediate

Use dict.fromkeys for ordered de-dup.

python
items = [1, 2, 2, 3, 4, 4, 5, 1]
unique = list(dict.fromkeys(items))
print(unique)

Advanced

1 program

Program 1

Matrix transpose

Advanced

Transpose a 2-D list using zip.

python
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
transposed = [list(row) for row in zip(*matrix)]
for row in transposed:
    print(row)