Learn Python
>>> data_structures.lists

Lists in Python

#concept

A list is Python's core ordered, mutable collection — you can change, add, or remove items after creating it, unlike a string or tuple. Lists are written with square brackets and can hold any mix of types, including other lists.

#how it works
1

Create with square brackets

[item1, item2, ...] builds a list holding any values, in order.

2

Access by index

fruits[0] gets the first item — indexing starts at 0.

3

Lists are mutable

Assigning to an index changes that item in place.

4

Negative indices count from the end

fruits[-1] is always the last item, however long the list is.

5

len() reports the size

Returns how many items the list currently holds.

#example
fruits = ["apple", "banana", "cherry"]
print(fruits[0])
print(fruits[-1])
 
fruits[1] = "blueberry"
print(fruits)
print(len(fruits))
Output
apple
cherry
['apple', 'blueberry', 'cherry']
3
1fruits[0] accesses the first item, "apple".
2fruits[-1] accesses the last item using a negative index, "cherry".
3fruits[1] = "blueberry" changes the second item in place — this only works because lists are mutable.
4Printing fruits shows the change took effect; len(fruits) confirms it still holds 3 items.
#use cases
Storing a collection of items that will grow, shrink, or change over the life of a program
Representing an ordered sequence, like a queue of tasks or a leaderboard
Building up a result one item at a time inside a loop
The default choice for 'a bunch of things' in Python, unless there's a specific reason to use something else

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.