PythonMastery

Unpack a row instead of indexing it

Replace row[0], row[1], row[2:] with one line: date, city, *temps = row. Names instead of positions, and the rest collected into a list.

When a row has a fixed start and a variable tail, name the parts in one assignment. The * collects everything that's left into a list.

Before

python
row = ["2026-09-29", "Pune", 31.4, 29.8, 27.1]

date = row[0]
city = row[1]
temps = row[2:]
print(date, city, max(temps))
output
2026-09-29 Pune 31.4

Three lines of indexes, and nothing tells the reader what position 1 was.

After

python
row = ["2026-09-29", "Pune", 31.4, 29.8, 27.1]

date, city, *temps = row
print(date, city, max(temps))
output
2026-09-29 Pune 31.4

It works from either end

The starred name can sit anywhere, and _ is the usual name for a part you don't need.

python
readings = [12, 15, 19, 22, 18, 14]

first, *_, last = readings
print(first, last)

*history, today = readings
print(len(history), today)
output
12 14
5 14

When not to use it

The starred part is always a new list. On a huge iterator that means holding every item in memory at once; use next() or itertools.islice there instead. And if a row might be too short, unpacking raises ValueError: see not enough values to unpack.

Learn it properly: Tuples: Lists That Can't Be Changed, Lists & Sequences