Need to turn task IDs and titles into a dictionary? Both lists are iterables: they supply items one by one. Python can use those items in a loop or pass them to a constructor, often without an indexing loop. section 5.1 section 5.3
An iterable can be converted to an iterator with iter(). Calling next() on that iterator retrieves successive items; after the last item, it raises StopIteration. A for loop creates and advances the iterator for you, then exits when it is exhausted. section 5.1.1
next(tasks_iterator) # output: 'task0' next(tasks_iterator) # output: 'task1' next(tasks_iterator) # output: 'task2' next(tasks_iterator) # ERROR: StopIteration
Successive next() calls make item-by-item retrieval—and the final StopIteration—visible. section 5.1.1
Strings yield characters. Lists, tuples, dictionaries, sets, and ranges are also iterable. To check an unfamiliar object, try iter(obj): it succeeds for a list but raises TypeError for an integer. A dictionary yields its keys by default; use its values or items when those are what you need. section 2.2.3 section 5.1.2 section 5.5
Build a dictionary from two iterables
Given id_numbers = [101, 102, 103] and titles = ["Laundry", "Homework", "Soccer"], use dict(zip(id_numbers, titles)). The result is {101: "Laundry", 102: "Homework", 103: "Soccer"}. zip pairs corresponding items; dict consumes those pairs. This avoids a loop that indexes both lists. section 5.1 section 5.1.3
Choose the consumer that matches the result you want:
- Use list(), tuple(), or set() to create a container from an iterable; use dict() when its iterable supplies key–value pairs. For example, list(range(10)) creates a list of numbers, while set((-2, 4, 0, 2, 4, 2)) removes duplicates. section 5.1.3
- Use list(map(float, numbers_str)) to turn an iterable of numeric strings into a list of floats. If you need to change items while building a container, consider a comprehension; otherwise, try the constructor first. section 5.1.3 section 5.5
- Use ", ".join(strings) to combine an iterable of strings. The separator owns join(), so the input need not be a list. section 2.3.2
Do not assume that similar-looking objects are interchangeable: range(1, 10, 2) is iterable, but slice(1, 10, 2) is not. A range can feed list() or a for loop; a slice selects part of a sequence. section 4.1.2






