Lists & loops
Iterate, transform, and combine sequences with readable loop patterns.
How to Merge Two Lists in Python
Merge two Python lists into a single combined list by appending each element with a simple loop, achieving the same result as the + operator.
def merge_lists(list_a, list_b):
merged = []
for item in list_a:
merged.append(item)
for item in list_b:
merged.append(item)
return merged
if __name__ == "__main__":
fruits = ["apple", "banana"]
vegetables = ["carrot", "spinach"]
result = merge_lists(fruits, vegetables)
…
How to Zip Two Lists into Pairs in Python
Combine two lists element-wise into a list of tuples using Python's built-in zip() function.
def zip_lists_into_pairs(list1, list2):
pairs = list(zip(list1, list2))
return pairs
if __name__ == "__main__":
fruits = ["apple", "banana", "cherry"]
quantities = [3, 5, 2]
result = zip_lists_into_pairs(fruits, quantities)
print(result)
Browse by section
Each section groups closely related Python snippets.
Lists & loops — Python code examples
What you will find here
This page collects lists & loops snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
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.