A list is Python's go-to data structure for storing an ordered collection of items — numbers, strings, even other lists, all in one variable. Lists are one of the most-used types in the language because they're flexible: unlike a tuple, they can grow, shrink, and change after you create them.
Creating and Indexing Lists
fruits = ["apple", "banana", "cherry", "date"]
print(fruits[0]) # first item
print(fruits[-1]) # last item
print(fruits[1:3]) # a slice: banana, cherry
print(len(fruits)) # number of items
Output:
apple
date
['banana', 'cherry']
4
Indexing and slicing work exactly like they do on strings — including negative indices and the [start:stop:step] syntax covered in String Slicing.
Lists Are Mutable
Unlike strings, you can change a list's contents in place — no new object is created:
fruits = ["apple", "banana", "cherry"]
fruits[1] = "blueberry" # replace an item directly
fruits.append("date") # add to the end
fruits.insert(0, "apricot") # insert at a specific position
print(fruits)
fruits.remove("cherry") # remove by value
popped = fruits.pop() # remove and return the last item
print(fruits, "| popped:", popped)
Output:
['apricot', 'apple', 'blueberry', 'cherry', 'date']
['apricot', 'apple', 'blueberry'] | popped: date
Sorting and Searching
numbers = [5, 2, 9, 1, 7]
numbers.sort() # sorts in place
print(numbers)
numbers.sort(reverse=True) # descending order
print(numbers)
print(3 in numbers) # membership check
print(numbers.index(9)) # find the position of a value
Output:
[1, 2, 5, 7, 9]
[9, 7, 5, 2, 1]
False
1
Note that .sort() modifies the list in place and returns None — if you want a new sorted list without touching the original, use the built-in sorted(numbers) instead.
Looping Over a List
Lists are iterated with a plain for loop, and for more compact transformations, Python offers list comprehensions. For a complete worked example that loops over a list to find a value, see finding the largest number in a list.
