Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Find the intersection and union of two sets

Compute the intersection and union of two collections, returning sorted lists for deterministic output.

Python practice16 minDictionaries & SetsIntermediateLast updated March 18, 2026

Problem statement

Given two iterables (e.g., lists or sets) of hashable items, compute their intersection and union. Return a tuple (intersection_list, union_list) where both lists are sorted in ascending order. Duplicate items in input should be ignored because sets only contain unique elements. Sorting ensures deterministic output for tests and users.

Task

Write a function that accepts two iterables, computes their set intersection and union, and returns both as sorted lists.

Examples

Intersection and union of small sets

Input

set_intersection_union({1, 2, 3}, {2, 3, 4})

Output

([2, 3], [1, 2, 3, 4])

Intersection is {2,3} and union is {1,2,3,4}, returned as sorted lists.

Input format

Two iterables containing hashable items (e.g., lists, sets, tuples).

Output format

A tuple of two lists: (sorted_intersection, sorted_union).

Constraints

Do not assume inputs are sets; convert them to sets first. Sorting should use the default order for the item types (numbers or strings).

Samples

Sample 1

Input

set_intersection_union([3, 1, 2], [2, 4])

Output

([2], [1, 2, 3, 4])

Duplicates removed; intersection [2], union [1,2,3,4].