List and dictionary programs · Python Programs
List programs practise loops, comparisons and nested structures. Each page gives the problem, a worked solution, the output, a line-by-line explanation and a shorter version using built-in tools.
All 6
Python Program to Find the Largest Number in a List
The largest number in a list is `max(numbers)`. To do it by hand, start with the first item and replace it whenever you meet a bi…
Python Program to Find the Second Largest Number in a ListTo find the second largest number, remove duplicates, sort, and take the second from the end: `sorted(set(nums))[-2]`.
Python Program to Calculate the Average of a ListThe average of a list is the sum divided by the count: `sum(numbers) / len(numbers)`.
Python Program to Flatten a Nested List of Any DepthTo flatten a list of lists one level, use `[x for row in nested for x in row]`. For unknown depth, use a recursive function.
Python Program to Transpose a MatrixTo transpose a matrix stored as a list of lists, use `[list(row) for row in zip(*matrix)]`: rows become columns and columns becom…
Python Program to Merge Two Sorted ListsTo merge two sorted lists, compare their front items repeatedly and take the smaller one. In Python, `list(heapq.merge(a, b))` do…
Explore more
Python ReferencePython ComparisonsPython How-To GuidesPython Cheat SheetsPython glossaryPython tutorial