Write a function `flatten(nested)` that takes a list of lists and returns a flat list using `itertools.chain.from_iterable`. Example: `flatten([[1, 2], [3], [4, 5, 6]])` → `[1, 2, 3, 4, 5, 6]`.
Write a function `first_n(gen, n)` that returns the first `n` elements from any generator or iterator as a list, using `itertools.islice`. Example: `first_n((x**2 for x in range(100)), 5)` → `[0, 1, 4, 9, 16]`.
from itertools import islice
def first_n(gen, n):
pass
print(first_n((x**2 for x in range(100)), 5))
# [0, 1, 4, 9, 16]
Solution
from itertools import islice
def first_n(gen, n):
return list(islice(gen, n))
print(first_n((x**2 for x in range(100)), 5))
# [0, 1, 4, 9, 16]
Write a function `round_robin(tasks)` that takes a list of task names and returns an infinite cycle iterator over them using `itertools.cycle`. Then use `itertools.islice` to extract the first 8 assignments. Example: `list(islice(round_robin(["A", "B", "C"]), 8))` → `["A", "B", "C", "A", "B", "C", "A", "B"]`.
Write a function `merge_sequences(seq1, seq2, seq3, fill=None)` that combines three sequences element by element using `itertools.zip_longest`, with `fill` as the missing value. Return the result as a list of tuples. Example: `merge_sequences([1,2,3], ["a","b"], [True], fill=0)` → `[(1,"a",True), (2,"b",0), (3,0,0)]`.
Write a function `running_max(numbers)` that returns a list where each element is the maximum value seen so far in `numbers`. Use `itertools.accumulate`. Example: `running_max([3, 1, 4, 1, 5, 9, 2, 6])` → `[3, 3, 4, 4, 5, 9, 9, 9]`.
Write a function `grid(rows, cols)` that returns all (row, col) coordinate pairs for a grid of given dimensions, using `itertools.product`. Example: `grid(2, 3)` → `[(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]`.
Write a function `all_pairs(items)` that returns all unique unordered pairs of items (no item paired with itself). Use `itertools.combinations`. Example: `all_pairs(["A", "B", "C", "D"])` → `[("A","B"), ("A","C"), ("A","D"), ("B","C"), ("B","D"), ("C","D")]`.
Write a function `group_by(items, key_func)` that groups a list of items by a key function and returns a dict mapping each key to a list of items. Use `itertools.groupby` (remember to sort first). Example: with `items = ["apple", "ant", "banana", "bear", "cherry"]` and `key_func = lambda w: w[0]`, result is `{"a": ["ant", "apple"], "b": ["banana", "bear"], "c": ["cherry"]}`.
Write a function `read_until_empty(lines)` that takes a list of strings and returns only the lines before the first empty string (or line containing only whitespace), using `itertools.takewhile`. Example: `read_until_empty(["hello", "world", "", "more", "text"])` → `["hello", "world"]`.
Write a function `generate_passwords(chars, length, count)` that generates `count` unique random passwords of `length` characters, where each password is built from `itertools.product(chars, repeat=length)`. Return the first `count` products as joined strings. Example: `generate_passwords("ab", 2, 4)` → `["aa", "ab", "ba", "bb"]` (use product in order, no randomness needed).
from itertools import product, islice
def generate_passwords(chars, length, count):
return ["".join(p) for p in islice(product(chars, repeat=length), count)]
print(generate_passwords("ab", 2, 4))
# ["aa", "ab", "ba", "bb"]
No split tab
Cookie preferences
We use necessary cookies to run the site. With your permission, we can also save your site preferences and use analytics and advertising cookies to understand usage and support the project.
Open tools in tabs.Exercises, IDE tools, and trainers stay available as site tabs.
Switch without losing context.Move between explanations, code, and utilities while keeping your place.
Use the sidebar as your map.The left panels hold navigation, settings, files, libraries, and tool controls.
PythonJavaScriptSQLite
One IDE, three practical modes
Python in the browser.Run small scripts, try libraries, and practice API requests without installing anything.
JavaScript for quick experiments.Test browser-friendly code and compare ideas next to your learning materials.
SQLite for data practice.Open the database explorer to inspect tables, write queries, and learn SQL workflows locally.
TopicIDE
Work side by side with split tabs
Keep instructions visible.Open an exercise or reference page beside the IDE instead of jumping back and forth.
Compare tools while you learn.Place regex checks, explanations, and code experiments next to each other when the task needs it.
Close the split when you are done.The workspace returns to a single focused tab, and your open site tabs remain available.
Code Typing Trainer
Or plain text
This trainer is designed for a physical keyboard.Open this section on a laptop or desktop with a wide screen. Touch typing practice will not work correctly on a phone.
Speed: 0 chars/min
0 words/min
Best speed (60s): 0 chars/min
0 words/min
Errors: 0
Total time: 0.0 s
To practice touch typing, avoid looking at your physical keyboard.