Back to blog
← View series: python tutorials
← View series: python tutorials
~/blog
Data Structures - Lists
Apr 1, 2026•5 min read•By Mohammed Vasim
PythonProgrammingTutorialBeginner
Introduction
In this tutorial, you'll learn about Python lists - one of the most versatile and commonly used data structures. Lists allow you to store multiple items in a single variable, making it easy to work with collections of data.
What You'll Learn
- Creating lists
- Accessing list elements (indexing and slicing)
- Modifying lists (adding, removing, updating)
- List methods and operations
- Iterating over lists
- List comprehension
What are Lists?
Lists are ordered collections of items. They can hold values of any type and are mutable (you can change them after creation).
python
# Creating a list
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, True]
print(fruits) # Output: ['apple', 'banana', 'cherry']
print(numbers) # Output: [1, 2, 3, 4, 5]
print(mixed) # Output: [1, 'hello', 3.14, True]Creating Lists
python
# Empty list
empty = []
print(empty) # Output: []
# List with initial values
colors = ["red", "green", "blue"]
# Using the list() constructor
numbers = list(range(5)) # [0, 1, 2, 3, 4]
print(numbers)
# List with repeated values
zeros = [0] * 5 # [0, 0, 0, 0, 0]
print(zeros)Accessing Elements
Indexing
Each element has an index starting from 0:
python
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
print(fruits[0]) # apple (first)
print(fruits[1]) # banana (second)
print(fruits[2]) # cherry (third)
# Negative indexing (from the end)
print(fruits[-1]) # elderberry (last)
print(fruits[-2]) # date (second to last)Slicing
Get a portion of the list:
python
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[2:6]) # [2, 3, 4, 5] (index 2 to 5)
print(numbers[:5]) # [0, 1, 2, 3, 4] (start to 4)
print(numbers[5:]) # [5, 6, 7, 8, 9] (5 to end)
print(numbers[::2]) # [0, 2, 4, 6, 8] (every other)
print(numbers[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] (reversed)Modifying Lists
Changing Elements
python
fruits = ["apple", "banana", "cherry"]
fruits[0] = "orange"
print(fruits) # ['orange', 'banana', 'cherry']
fruits[-1] = "grape"
print(fruits) # ['orange', 'banana', 'grape']Adding Elements
python
fruits = ["apple", "banana"]
# append() - add to end
fruits.append("cherry")
print(fruits) # ['apple', 'banana', 'cherry']
# insert() - add at specific position
fruits.insert(1, "orange")
print(fruits) # ['apple', 'orange', 'banana', 'cherry']
# extend() - add multiple items
fruits.extend(["grape", "melon"])
print(fruits) # ['apple', 'orange', 'banana', 'cherry', 'grape', 'melon']
# Using + operator
fruits = fruits + ["kiwi", "lime"]
print(fruits) # Adds to the endRemoving Elements
python
fruits = ["apple", "banana", "cherry", "banana"]
# remove() - removes first occurrence
fruits.remove("banana")
print(fruits) # ['apple', 'cherry', 'banana']
# pop() - removes and returns last item
item = fruits.pop()
print(item) # banana
print(fruits) # ['apple', 'cherry']
# pop() with index
fruits.pop(0)
print(fruits) # ['cherry']
# clear() - remove all items
fruits.clear()
print(fruits) # []
# del statement
fruits = ["apple", "banana", "cherry"]
del fruits[1]
print(fruits) # ['apple', 'cherry']List Methods
Python provides many built-in methods for lists:
python
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
# Sorting
numbers.sort()
print(numbers) # [1, 1, 2, 3, 4, 5, 6, 9]
# Reverse sorting
numbers.sort(reverse=True)
print(numbers) # [9, 6, 5, 4, 3, 2, 1, 1]
# Reverse without sorting
numbers.reverse()
print(numbers) # [1, 1, 2, 3, 4, 5, 6, 9]
# Index of first occurrence
print(numbers.index(5)) # 4
# Count occurrences
print(numbers.count(1)) # 2
# Copy a list
numbers_copy = numbers.copy()
print(numbers_copy) # [1, 1, 2, 3, 4, 5, 6, 9]Common Methods Table
| Method | Description | Example |
|---|---|---|
append(item) | Add to end | list.append(x) |
insert(i, x) | Insert at index | list.insert(0, x) |
extend(iterable) | Add multiple | list.extend([x, y]) |
remove(x) | Remove first x | list.remove(x) |
pop(i) | Remove at index | list.pop() |
clear() | Remove all | list.clear() |
index(x) | Find index | list.index(x) |
count(x) | Count occurrences | list.count(x) |
sort() | Sort in place | list.sort() |
reverse() | Reverse in place | list.reverse() |
Iterating Over Lists
python
fruits = ["apple", "banana", "cherry"]
# Basic for loop
for fruit in fruits:
print(fruit)
# With index using enumerate()
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
# Using range
for i in range(len(fruits)):
print(fruits[i])List Comprehension
A concise way to create lists:
Basic Syntax
python
# Instead of:
squares = []
for i in range(10):
squares.append(i ** 2)
# Use comprehension:
squares = [i ** 2 for i in range(10)]
print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]With Condition
python
# Even numbers only
evens = [i for i in range(10) if i % 2 == 0]
print(evens) # [0, 2, 4, 6, 8]
# With if-else
labels = ["even" if i % 2 == 0 else "odd" for i in range(5)]
print(labels) # ['even', 'odd', 'even', 'odd', 'even']Practical Examples
python
# Uppercase strings
fruits = ["apple", "banana", "cherry"]
upper = [f.upper() for f in fruits]
print(upper) # ['APPLE', 'BANANA', 'CHERRY']
# Flatten a 2D list
matrix = [[1, 2], [3, 4], [5, 6]]
flat = [num for row in matrix for num in row]
print(flat) # [1, 2, 3, 4, 5, 6]Common Patterns
Finding Sum, Min, Max
python
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
print(sum(numbers)) # 31
print(min(numbers)) # 1
print(max(numbers)) # 9
print(len(numbers)) # 8Checking if Item Exists
python
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("grape" in fruits) # FalseSummary
In this tutorial, you learned:
- ✅ Creating and initializing lists
- ✅ Accessing elements (indexing and slicing)
- ✅ Modifying lists (adding, removing, updating)
- ✅ List methods (sort, reverse, etc.)
- ✅ Iterating over lists
- ✅ List comprehension
🧑💻 Practice Exercise
Create a program that:
- Creates a list of 5 numbers
- Finds and prints the sum, average, max, and min
- Removes the largest number and prints the updated list
- Creates a new list with each number doubled
Click to see solution
python
# List operations practice
numbers = [10, 25, 5, 40, 15]
# Find sum, average, max, min
total = sum(numbers)
average = total / len(numbers)
maximum = max(numbers)
minimum = min(numbers)
print("--- Statistics ---")
print(f"Sum: {total}")
print(f"Average: {average}")
print(f"Max: {maximum}")
print(f"Min: {minimum}")
# Remove largest number
numbers.remove(max(numbers))
print(f"\nAfter removing max: {numbers}")
# Double each number
doubled = [num * 2 for num in numbers]
print(f"Doubled: {doubled}")Output:
--- Statistics ---
Sum: 95
Average: 19.0
Max: 40
Min: 5
After removing max: [10, 25, 5, 15]
Doubled: [20, 50, 10, 30]
What's Next
In the next tutorial, we'll learn about Dictionaries & Tuples - more ways to organize and store data.