Python Code
Samples
Medium snippets you can copy, study, and run in the browser editor.
Container With Most Water: Two-Pointer Solution in Python
Find the maximum water a container can hold from a list of heights using an efficient two-pointer technique in O(n) time.
from typing import List
def max_water_container(heights: List[int]) -> int:
left, right = 0, len(heights) - 1
max_area = 0
while left < right:
width = right - left
height = min(heights[left], heights[right])
area = width * height
max_area = max(max_area, area)
…
Find All Triplets with Sum Zero in Python
This code finds all unique triplets in an array that sum to zero using a sorted array and two-pointer technique.
def find_triplets(nums):
nums.sort()
n = len(nums)
triplets = []
for i in range(n - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
left, right = i + 1, n - 1
while left < right:
total = nums[i] + nums[left] + nums[right]
if total == 0:
…
Find the Duplicate Number in Python Using Floyd's Cycle Detection
Detects the duplicate integer in an array of n+1 numbers (values 1 to n) in O(n) time and O(1) space using Floyd's cycle detection algorithm applied to a linked-list model.
def find_duplicate(nums):
slow = nums[0]
fast = nums[0]
# Phase 1: Find intersection point of the cycle
while True:
slow = nums[slow]
fast = nums[nums[fast]]
if slow == fast:
break
# Phase 2: Find the start of the cycle (the duplicate)
slow = nums[0…
How to Find Four Sum Quadruplets in Python (Sorted Demo)
Find all unique quadruplets in a sorted array that sum to a target, with duplicate skipping.
def four_sum(nums, target):
nums.sort()
result = []
n = len(nums)
for i in range(n - 3):
if i > 0 and nums[i] == nums[i - 1]:
continue
for j in range(i + 1, n - 2):
if j > i + 1 and nums[j] == nums[j - 1]:
continue
left, right = j + 1…
How to Find Intersection of Two Sorted Interval Lists in Python
A two-pointer algorithm that finds all overlapping intervals between two sorted lists of intervals.
def interval_intersection(list1, list2):
i = j = 0
result = []
while i < len(list1) and j < len(list2):
# Find the overlap between current intervals
lo = max(list1[i][0], list2[j][0])
hi = min(list1[i][1], list2[j][1])
# If there's an overlap, add it to result
…
How to Solve the Trapping Rain Water Problem in Python
Compute the total water trapped between elevation bars using a two-pointer O(n) algorithm.
def trap(height):
if not height:
return 0
left, right = 0, len(height) - 1
left_max, right_max = 0, 0
water = 0
while left < right:
if height[left] < height[right]:
if height[left] >= left_max:
left_max = height[left]
else:
…
How to Sort Colors (Dutch National Flag) in Python
In-place sorting of a list of 0s, 1s, and 2s using the Dutch National Flag algorithm with O(n) time and O(1) space.
def sort_colors(nums):
low, mid, high = 0, 0, len(nums) - 1
while mid <= high:
if nums[mid] == 0:
nums[low], nums[mid] = nums[mid], nums[low]
low += 1
mid += 1
elif nums[mid] == 1:
mid += 1
else: # nums[mid] == 2
nums[mid], n…
Browse by section
Each section groups closely related Python snippets.
Guide: free Python code samples library
Copy-ready Python snippets for learners and developers
PythonSkillset code samples are short, focused examples organised by topic and difficulty. Every snippet is server-rendered HTML — readable by search engines and easy to copy. Open any sample, read the notes, copy the code, then press Try in editor to run it in the browser with Pyodide.
How to use this library
- Pick a topic section — strings, lists, files, functions, and more
- Open a sample, read How it works, and copy the code block
- Run it in the IDE, tweak values, then take a related quiz or tutorial lesson
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.