Indexing


15 min.   |   Intermediate  

Zero-Based Indexing

Python uses zero-based indexing, meaning the first element has index 0.

Negative Indexing

Negative indices start from the end of the sequence, with the last element having index -1.

Multi-Level Indexing

Access nested structures by chaining indices.

Slicing

Use start:stop to get a portion of a sequence. The stop index is exclusive (not including).

Indexing: Exercises

Tip Indexing: Exercise 1

You are given a list of exam_scores. Compute the median of this dataset using indexing.

Tip Indexing: Exercise 2

You are given a dictionary course. The “course_id” contains a long alphanumeric identifier.

Slice the 5th to 14th characters of “course_id”.

Tip Indexing: Exercise 3

Sort the list of data below and return the number that sits 25\% of the way into the sorted list.

Tip Indexing: Exercise 4

Given a 30 \times 30 matrix represented as a list of lists. Access the element a_{15,7}.

Tip Indexing: Exercise 5

A product_code was entered with an extra character “#”. The “#” is an error and it is located at the 27th index of the string.

Create a cleaned version of the string where the “#” is removed.