Python Code
Samples
Easy snippets you can copy, study, and run in the browser editor.
How to Merge Two Sorted Lists in Python
Merge two sorted lists into one sorted list using a two-pointer loop, then extend with remaining elements.
def merge_sorted_lists(list1, list2):
merged = []
i = j = 0
while i < len(list1) and j < len(list2):
if list1[i] <= list2[j]:
merged.append(list1[i])
i += 1
else:
merged.append(list2[j])
j += 1
merged.extend(list1[i:])
merged…
How to Reverse a List in Place Without Using reverse() in Python
A two-pointer while loop swaps elements from both ends toward the center to reverse a list in place without creating a copy.
def reverse_list_in_place(lst):
left = 0
right = len(lst) - 1
while left < right:
lst[left], lst[right] = lst[right], lst[left]
left += 1
right -= 1
if __name__ == "__main__":
my_list = [1, 2, 3, 4, 5]
print("Original:", my_list)
reverse_list_in_place(my_list)
prin…
Find Median of Two Sorted Arrays in Python
Merges two sorted arrays with a two-pointer walk and returns the median of the combined sorted sequence.
def median_of_two_sorted_arrays(nums1, nums2):
merged = []
i = j = 0
while i < len(nums1) and j < len(nums2):
if nums1[i] <= nums2[j]:
merged.append(nums1[i])
i += 1
else:
merged.append(nums2[j])
j += 1
merged.extend(nums1[i:])
merged.…
Find the Equilibrium Index of a List in Python
Find every index in a list where the sum of elements to its left equals the sum to its right, using a single pass.
def find_equilibrium_indexes(arr):
total = sum(arr)
left_sum = 0
indexes = []
for i, num in enumerate(arr):
total -= num
if left_sum == total:
indexes.append(i)
left_sum += num
return indexes
if __name__ == "__main__":
test = [1, 2, 3, -1, 2, 3]
result =…
How to Sort Array by Parity (Even Before Odd) in Python
Rearrange an array so all even numbers appear before all odd numbers using a simple two-list partition approach.
def sort_array_by_parity(nums):
"""
Rearrange the array so that all even integers come first,
followed by all odd integers. The order within even and odd
groups is not required to be sorted.
"""
even = []
odd = []
for num in nums:
if num % 2 == 0:
even.append(nu…
Merge Two Sorted Arrays Without Extra Space in Python
Merge two sorted arrays in-place from the end, using the trailing zeros in the first array to avoid extra space.
def merge_sorted(arr1, arr2):
m, n = len(arr1), len(arr2)
i, j = m - 1, n - 1
while j >= 0:
if i >= 0 and arr1[i] > arr2[j]:
arr1[i + j + 1] = arr1[i]
i -= 1
else:
arr1[i + j + 1] = arr2[j]
j -= 1
return arr1
if __name__ == "__main__":
…
Move Zeroes to End in Python Maintaining Order
In-place algorithm that moves all zeroes to the end of a list while preserving the relative order of non-zero elements.
def move_zeroes(nums):
non_zero_index = 0
for i in range(len(nums)):
if nums[i] != 0:
nums[non_zero_index], nums[i] = nums[i], nums[non_zero_index]
non_zero_index += 1
return nums
if __name__ == "__main__":
example = [0, 1, 0, 3, 12]
result = move_zeroes(example)
…
Rearrange array alternately max min in Python
Rearranges a sorted list so its elements alternate between the current maximum and current minimum using two pointers in O(n) time.
def rearrange_alternately(arr):
"""
Rearrange sorted array so elements alternate: max, min, next max, next min...
Returns a new list in O(n) time using O(n) space.
"""
n = len(arr)
result = []
left, right = 0, n - 1
while left <= right:
if left == right:
result.appen…
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.