Lists Basics
Learn how to store and work with multiple items using Python lists
Lists Basics
What is a List?
Think of a list like a shopping list. Instead of storing one item, you can store many items together in order.
A list in Python is a collection that holds multiple values. You can put numbers, text, or even mix different types of data in one list.
Why use lists?
- Store related items together (like names of students, prices of products)
- Keep data organized in order
- Easy to add, remove, or change items
- Work with many values at once
Creating a List
You create a list using square brackets. Put your items inside, separated by commas.
fruits = ["apple", "banana", "orange"]
print(fruits)What happens here: Python creates a list with 3 fruit names. The order matters - apple is first, banana is second, orange is third.
Different types of lists:
- List of numbers - Store ages, prices, scores
ages = [25, 30, 18, 42]- List of text - Store names, cities, colors
colors = ["red", "blue", "green"]- Mixed list - Combine different types (not common but possible)
mixed = ["John", 25, True]- Empty list - Start with nothing, add items later
empty = []Accessing Items in a List
Each item in a list has a position number called an index. Python starts counting from 0, not 1.
fruits = ["apple", "banana", "orange"]
print(fruits[0])
print(fruits[1])What this does:
- fruits[0] gives you "apple" (first item)
- fruits[1] gives you "banana" (second item)
Important: The first item is at position 0, second item at position 1, and so on.
Negative Indexing
You can count from the end using negative numbers. This is helpful when you want the last item.
fruits = ["apple", "banana", "orange", "grape"]
print(fruits[-1])
print(fruits[-2])What happens:
- fruits[-1] gives you "grape" (last item)
- fruits[-2] gives you "orange" (second from end)
Changing List Items
Lists are flexible. You can change any item by accessing its position and giving it a new value.
fruits = ["apple", "banana", "orange"]
fruits[1] = "mango"
print(fruits)What this does: Takes the second item (banana) and replaces it with "mango". Now the list is ["apple", "mango", "orange"].
Checking List Length
Use the len() function to count how many items are in your list.
fruits = ["apple", "banana", "orange"]
count = len(fruits)
print(count)Result: Shows 3 because there are 3 items in the list.
Checking if Item Exists
Use the word "in" to check if something is in your list. This gives you True or False.
fruits = ["apple", "banana", "orange"]
print("apple" in fruits)
print("grape" in fruits)What happens:
- First line prints True (apple is in the list)
- Second line prints False (grape is not in the list)
Practice Example
The scenario: You're managing a small library. You have 4 books and want to check information about them.
books = ["Python Basics", "Data Science", "Web Design", "AI Guide"]
print("Total books:", len(books))
print("First book:", books[0])
print("Last book:", books[-1])
books[2] = "JavaScript Guide"
print("Updated list:", books)
if "Python Basics" in books:
print("Python book available")What this program does:
- Creates a list of 4 book names
- Counts total books (shows 4)
- Shows first book (Python Basics)
- Shows last book (AI Guide)
- Changes third book from Web Design to JavaScript Guide
- Checks if Python Basics is available (it is, so prints message)
Key Points to Remember
Lists are containers that hold multiple items in order. You create them with square brackets and separate items with commas.
Python counts positions starting from 0, not 1. The first item is at index 0, second at index 1, and so on.
You can count from the end using negative numbers. The last item is -1, second from end is -2.
Lists can be changed after creation. You can update any item by accessing its position and assigning a new value.
Use len() to count items and "in" to check if something exists in the list.
Common Mistakes
Mistake 1: Forgetting Python starts at 0
numbers = [10, 20, 30]
print(numbers[1]) # This gives 20, not 10Mistake 2: Trying to access position that doesn't exist
fruits = ["apple", "banana"]
print(fruits[5]) # Error! Only 2 items existMistake 3: Forgetting square brackets when creating list
colors = "red", "blue" # This creates tuple, not list
colors = ["red", "blue"] # Correct wayWhat's Next?
Now you know list basics. In the next lesson, you'll learn advanced list operations like adding items, removing items, sorting, and more ways to work with lists.