androidengineers.Book a session

Python 03 · Collections

Lists, tuples, indexing, and slicing

articleSelf-paced

Learn the concept

A list is an ordered mutable collection. A tuple is an ordered collection whose element references cannot be reassigned. Use lists when you need to append or change items and tuples for fixed groupings such as a coordinate or a returned pair.

Indexing starts at zero; negative indexes count from the end. Slices create a new outer list, but nested mutable objects may still be shared. This distinction matters when modifying batches of records. Copying a container is not necessarily copying all the objects inside it.

Choose operations by intent: append adds one item, extend adds items from an iterable, and pop removes and returns an item. Avoid deleting from a list while iterating over it unless you explicitly account for changing indexes. A filtered new list is often clearer.

Run and inspect

items = ["a", "b", "c"]
items.append("d")
assert items[0] == "a"
assert items[-1] == "d"
assert items[1:3] == ["b", "c"]
point = (3, 4)
x, y = point
assert x + y == 7

Your exercise

Store five document IDs, take the first three as a batch, and remove a completed ID without changing iteration behavior unexpectedly.

Check your understanding

You can explain the difference between indexing one element and slicing several elements, including the excluded end index.

YOUR LEARNING JOURNEY

0 of 118 available lessons completed

Progress saved in this browser. No account needed.
Lists, tuples, indexing, and slicing | Agentic AI | Android Engineers