Skip to content
IT Vizag
← Lists, Dicts & Data

Lists

Lesson 1 of 9 · 1 min read

Store many values in order, and add, remove and sort them.

🎬 Video version coming soon — read it now

So far each variable held one value. A list holds many values, in order, under one name.

beaches = ["RK Beach", "Rushikonda", "Yarada", "Bheemili"]
marks = [78, 92, 65, 88]
mixed = ["Vizag", 2026, True]   # allowed, but usually keep one type
empty = []

Reading items

Positions (called indexes) start at 0, just like strings:

print(beaches[0])     # RK Beach
print(beaches[-1])    # Bheemili (last item)
print(beaches[1:3])   # ['Rushikonda', 'Yarada']
print(len(beaches))   # 4

Asking for beaches[10] gives an IndexError, because there is no item at position 10.

Changing a list

Unlike strings, lists can be changed in place:

beaches.append("Lawson's Bay")     # add to the end
beaches.insert(0, "Thotlakonda")   # add at a position
beaches.remove("Yarada")           # remove by value
last = beaches.pop()               # remove and return the last item
beaches[0] = "Sagar Nagar"         # replace an item
print(beaches)

Sorting and checking

marks = [78, 92, 65, 88]
marks.sort()                 # changes the list: [65, 78, 88, 92]
marks.sort(reverse=True)     # [92, 88, 78, 65]
top = sorted(marks)          # returns a NEW sorted list, original untouched

print(92 in marks)           # True
print(marks.count(78))       # 1
print(marks.index(88))       # position of 88

Quick maths on lists

print(sum(marks), max(marks), min(marks))
print(f"Average: {sum(marks) / len(marks):.1f}")
Tip: list.sort() returns None. Writing marks = marks.sort() throws your list away. Use marks.sort() on its own line, or marks = sorted(marks).

In one line

A list stores values in order; index from 0, change it with append, insert, remove and pop, and sort it with sort() or sorted().

Saved on this device. Sign in to keep progress everywhere.

Share with a friend