Reference library

Lists & loops

Iterate, transform, and combine sequences with readable loop patterns.

54 matches
Lists & loops easy

How to summarize and transform lists in Python

Compute count, sum, min, max, and average for a list and multiply each element by a factor using simple loops and built-in functions.

lists loops statistics
Python
def summarize(data):
    """Return a summary of a list: count, sum, min, max, average."""
    count = len(data)
    total = sum(data)
    minimum = min(data)
    maximum = max(data)
    average = total / count if count else 0
    return count, total, minimum, maximum, average


def multiply_elements(data, factor=2):
 …
13 0 Open
Lists & loops easy

How to unzip a list of pairs into two lists in Python

Split a list of (a, b) tuples into two separate lists by iterating with a for loop and appending each element to its own output list.

lists tuples loops
Python
def unzip(pairs):
    """Split a list of (a, b) pairs into two separate lists."""
    if not pairs:
        return [], []
    
    firsts = []
    seconds = []
    for a, b in pairs:
        firsts.append(a)
        seconds.append(b)
    
    return firsts, seconds


if __name__ == "__main__":
    pairs = [(1, 'a'), (…
14 0 Open
Lists & loops easy

Intersection of Two Lists Preserving Order in Python

This code returns the common elements between two lists while preserving the order they appear in the first list, filtering out duplicates.

lists intersection order
Python
def intersection_preserving_order(list1, list2):
    """
    Return the intersection of two lists while preserving the order
    of elements as they appear in list1.
    """
    set2 = set(list2)
    result = []
    seen = set()
    
    for item in list1:
        if item in set2 and item not in seen:
            resu…
15 0 Open
Lists & loops easy

Round Robin Merge Multiple Lists in Python

Merge multiple lists by taking one element from each in turn, stopping when all lists are exhausted.

lists merge interleave
Python
from itertools import cycle

def round_robin_merge(*lists):
    """Merge multiple lists by taking one element from each in turn."""
    result = []
    max_len = max(len(lst) for lst in lists)
    
    for i in range(max_len):
        for lst in lists:
            if i < len(lst):
                result.append(lst[i])…
14 0 Open
Lists & loops easy

Separate Evens and Odds into Two Lists in Python

Split a list of numbers into two lists containing even and odd numbers using a simple loop and the modulo operator.

lists loops modulo
Python
def separate_evens_odds(numbers):
    evens = []
    odds = []
    for num in numbers:
        if num % 2 == 0:
            evens.append(num)
        else:
            odds.append(num)
    return evens, odds

if __name__ == "__main__":
    nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    evens, odds = separate_evens_odds(nu…
13 0 Open
Lists & loops easy

Symmetric difference between two lists in Python

Find elements present in exactly one of two lists, preserving original order, with a simple Python function.

lists sets symmetric-difference
Python
def symmetric_difference(list1, list2):
    """
    Return the symmetric difference of two lists.
    Elements present in exactly one of the lists, preserving order.
    """
    set1 = set(list1)
    set2 = set(list2)
    
    # Elements in list1 but not in list2
    diff1 = [x for x in list1 if x not in set2]
    # E…
14 0 Open

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.